Documentation
¶
Overview ¶
Package unit implements parsing for string values with units.
Index ¶
Constants ¶
const ( // K is 1024 byte K int64 = 1 << (10 * iota) // M is 1024 K M // G is 1024 M G // T is 1024 G T // P is 1024 T P // E is 1024 P E )
Variables ¶
var DefaultUnits = map[string]int64{ "B": 1, "K": K, "M": M, "G": G, "T": T, "P": P, "E": E, "kB": 1000, "KB": 1000, "MB": 1000 * 1000, "GB": 1000 * 1000 * 1000, "TB": 1000 * 1000 * 1000 * 1000, "PB": 1000 * 1000 * 1000 * 1000 * 1000, "EB": 1000 * 1000 * 1000 * 1000 * 1000 * 1000, }
DefaultUnits is the default unit mapping as used by many standard cli-tools.
Functions ¶
This section is empty.
Types ¶
type Unit ¶
type Unit struct {
// contains filtered or unexported fields
}
Unit is a map of unit names to conversion multipliers.
There must be a named unit that maps to 1.
The empty string is special: it defines the default unit, i.e., the multiplier applied to input without a unit suffix (see Set). It must alias one of the named units in the mapping. If no default unit is defined, input without a unit suffix is treated as multiplier 1. The default unit is never used for output; String always prints a named unit.
func MustNewUnit ¶
MustNewUnit is like NewUnit but panics if the mapping 'm' is not valid.
func NewUnit ¶
NewUnit returns a new Unit given a mapping 'm'.
The mapping is copied; later changes to 'm' do not affect the Unit.
func (*Unit) MustNewValue ¶
MustNewValue is like NewValue but panics if the new Value could not be generated.
type Value ¶
type Value struct {
// value is the integer value.
Value int64
// sign is the explicit sign given by the string converted to the
// integer.
ExplicitSign Sign
// set to false if this is the default value, true if the the option was given
IsSet bool
// contains filtered or unexported fields
}
Value is any value that can be represented by a unit.
Value implements flag.Value and flag.Getter.
func (Value) String ¶
String implements flag.Value.String and fmt.Stringer.
The value is printed using the named unit with the largest multiplier that divides it evenly. If several units share that multiplier, the shortest name is preferred, then the lexicographically smallest one. The default unit "" is never used. The result is deterministic and can be parsed back by Set.