Documentation
¶
Index ¶
- Constants
- Variables
- func Decode(text string) (int64, error)
- func DecodeBinary(text string) (int64, error)
- func DecodeDecimal(text string) (int64, error)
- func Encode(val int64) string
- func EncodeBinary(val int64) string
- func EncodeDecimal(val int64) string
- type Decoder
- type Encoder
- func (b *Encoder) Encode(val int64) string
- func (b *Encoder) EncodeBinary(valInt int64) string
- func (b *Encoder) EncodeDecimal(valInt int64) string
- func (b *Encoder) SetPrecision(precision int) *Encoder
- func (b *Encoder) SetTrimIntDecimal(trimIntZero bool) *Encoder
- func (b *Encoder) SetUseSpace(useSpace bool) *Encoder
Constants ¶
const ( KiB MiB GiB TiB PiB EiB )
Binary units (IEC 60027).
const ( KB = 1000 MB = KB * 1000 GB = MB * 1000 TB = GB * 1000 PB = TB * 1000 EB = PB * 1000 )
Decimal units (SI international system of units).
const ( DefaultPrecision = 2 DefaultTrimIntDecimal = true DefaultNoSpace = false )
Variables ¶
var ErrUnknownSuffix = errors.New("unknown suffix")
Functions ¶
func Decode ¶
Decode parses human-readable bytes string to bytes integer.
For example, "6 GiB" ("6 Gi" is also valid) will return 6442450944, and "6 GB" ("6 G" is also valid) will return 6000000000. The input case-insensitive, and the space is optional, so "6GiB" or "6GB" would produce the same output.
func DecodeBinary ¶
DecodeBinary parses human-readable bytes string to bytes integer.
For example, "6 GiB" ("6 Gi" is also valid) will return 6442450944. The input case-insensitive, and the space is optional, so "6GiB" would produce the same output.
func DecodeDecimal ¶
DecodeDecimal parses human-readable bytes string to bytes integer.
For example, "6 GB" ("6 G" is also valid) will return 6000000000. The input case-insensitive, and the space is optional, so "6GB" would produce the same output.
func Encode ¶
Encode formats bytes integer to human-readable string according to IEC 60027.
For example, 31323 bytes will return "30.59 KB".
The smallest supported value is "1 B", so precision will be ignored below "1 KiB" or "1 KB".
func EncodeBinary ¶
EncodeBinary formats bytes integer to human-readable string according to IEC 60027.
For example, 31323 bytes will return "30.59 KB".
The smallest supported value is "1 B", so precision will be ignored below "1 KB".
func EncodeDecimal ¶
EncodeDecimal formats bytes integer to human-readable string according to SI international system of units.
For example, 31323 bytes will return "31.32 KB".
The smallest supported value is "1 B", so precision will be ignored below "1 KiB".
Types ¶
type Decoder ¶
type Decoder struct{}
A Decoder decodes strings to byte values.
func (*Decoder) Decode ¶
Decode parses human-readable bytes string to bytes integer.
For example, "6 GiB" ("6 Gi" is also valid) will return 6442450944, and "6 GB" ("6 G" is also valid) will return 6000000000. The input case-insensitive, and the space is optional, so "6GiB" or "6GB" would produce the same output.
func (*Decoder) DecodeBinary ¶
DecodeBinary parses human-readable bytes string to bytes integer.
For example, "6 GiB" ("6 Gi" is also valid) will return 6442450944. The input case-insensitive, and the space is optional, so "6GiB" would produce the same output.
func (*Decoder) DecodeDecimal ¶
DecodeDecimal parses human-readable bytes string to bytes integer.
For example, "6 GB" ("6 G" is also valid) will return 6000000000. The input case-insensitive, and the space is optional, so "6GB" would produce the same output.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
A Encoder encodes byte values to strings.
func (*Encoder) Encode ¶
Encode formats bytes integer to human-readable string according to IEC 60027.
For example, 31323 bytes will return "30.59 KB".
The smallest supported value is "1 B", so precision will be ignored below "1 KiB" or "1 KB".
func (*Encoder) EncodeBinary ¶
EncodeBinary formats bytes integer to human-readable string according to IEC 60027.
For example, 31323 bytes will return "30.59 KB".
The smallest supported value is "1 B", so precision will be ignored below "1 KB".
func (*Encoder) EncodeDecimal ¶
EncodeDecimal formats bytes integer to human-readable string according to SI international system of units.
For example, 31323 bytes will return "31.32 KB".
The smallest supported value is "1 B", so precision will be ignored below "1 KiB".
func (*Encoder) SetPrecision ¶
SetPrecision instructs the encoder to include the given number of decimal places.
The special precision -1 uses the smallest number of digits necessary.
func (*Encoder) SetTrimIntDecimal ¶
SetTrimIntDecimal instructs the encoder to remove the decimal if the result is an integer (ends with ".00").
func (*Encoder) SetUseSpace ¶
SetUseSpace instructs the encoder to separate the value and suffix with a space.