Documentation ¶
Overview ¶
Package bytesize provides functionality for measuring and formatting byte sizes.
You can also perfom mathmatical operation with ByteSize's and the result will be a valid ByteSize with the correct size suffix.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // Use long units, such as "megabytes" instead of "MB". LongUnits bool = false // String format of bytesize output. The unit of measure will be appended // to the end. Uses the same formatting options as the fmt package. Format string = "%.2f" )
Functions ¶
This section is empty.
Types ¶
type ByteSize ¶
type ByteSize uint64
ByteSize represents a number of bytes
func New ¶
New returns a new ByteSize type set to s.
Example ¶
b := bytesize.New(1024) fmt.Printf("%s", b)
Output: 1.00KB
Example (Math) ¶
b1 := bytesize.New(1024) b2 := bytesize.New(4096) sum := b1 + b2 fmt.Printf("%s", sum)
Output: 5.00KB
func Parse ¶
Parse parses a byte size string. A byte size string is a number followed by a unit suffix, such as "1024B" or "1 MB". Valid byte units are "B", "KB", "MB", "GB", "TB", "PB" and "EB". You can also use the long format of units, such as "kilobyte" or "kilobytes".
Example ¶
b, _ := bytesize.Parse("1024 GB") fmt.Printf("%s\n", b) b, _ = bytesize.Parse("3 petabytes") fmt.Printf("%s\n", b) bytesize.LongUnits = true bytesize.Format = "%.0f " fmt.Printf("%s\n", b)
Output: 1.00TB 3.00PB 3 petabytes
func (ByteSize) Format ¶
Returns a string representation of b with the specified formatting and units.
Example ¶
Demonstrates using different output formatting and units.
b := 1 * bytesize.TB // Create a new 1 terabyte ByteSize. fmt.Printf("%s\n", b) fmt.Printf("%s\n", b.Format("%.8f ", "petabyte", true))
Output: 1.00TB 0.00097656 petabytes
func (*ByteSize) Get ¶
func (b *ByteSize) Get() interface{}
Satisfy the flag package Getter interface.
func (ByteSize) String ¶
String returns the string form of b using the package global Format and LongUnits options.
func (*ByteSize) UnmarshalText ¶
Satisfy the encoding.TextUnmarshaler interface.