Documentation
¶
Overview ¶
Package datasize provides named units, parsing and formatting for sizes of data in bytes, and a flag value that accepts sizes such as "6MB".
Index ¶
Constants ¶
const ( KB = 1 << 10 MB = 1 << 20 GB = 1 << 30 )
Units of data size. They are powers of 1024, so 6*MB is 6*1024*1024 bytes. They are untyped constants, so they can be used with any integer type.
Variables ¶
This section is empty.
Functions ¶
func Format ¶
Format returns the size in the largest unit that represents it exactly, for example "6MB" for 6291456 and "1500B" for 1500. Parse reads the result back as the same size. A negative size is not a valid data size, and it is returned as a plain number.
func Parse ¶
Parse parses a data size such as "6MB", "512KB" or "6291456" and returns it in bytes. A number without a unit is a number of bytes. The units are B, KB, MB and GB, and KiB, MiB and GiB are accepted as other names for KB, MB and GB. Units are powers of 1024 and are not case sensitive. Fractions and negative sizes are not accepted.
Types ¶
type Value ¶
type Value[T Integer] struct { // contains filtered or unexported fields }
Value is a flag value for a data size. It implements the Value interfaces of both the flag and the pflag packages, and stores the size in bytes in an integer variable. It accepts everything that Parse accepts. It also accepts every number that the integer flags of the flag and pflag packages accept, with the same meaning, so a flag that used to take a number of bytes keeps working when it is changed to a Value.
func NewValue ¶
NewValue returns a Value that stores the size in target. It sets target to defaultSize, in the same way as flag.IntVar.
func (*Value[T]) Get ¶
Get returns the current size in the type of the target variable. It makes Value a flag.Getter, as the integer flags are.