package bytesize
import "github.com/inhies/go-bytesize"
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 ¶
- Variables
- type ByteSize
- func New(s float64) ByteSize
- func Parse(s string) (ByteSize, error)
- func (b ByteSize) Format(format string, unit string, longUnits bool) string
- func (b *ByteSize) Get() interface{}
- func (b *ByteSize) Set(s string) error
- func (b ByteSize) String() string
- func (b *ByteSize) Type() string
- func (b *ByteSize) UnmarshalText(text []byte) error
Examples ¶
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" )
Types ¶
type ByteSize ¶
type ByteSize uint64
ByteSize represents a number of bytes
Byte size size suffixes.
func New ¶
New returns a new ByteSize type set to s.
Code:
Output: Code:
Output:Example¶
{
b := bytesize.New(1024)
fmt.Printf("%s", b)
// Output:
// 1.00KB
}
1.00KB
Example (Math)¶
{
b1 := bytesize.New(1024)
b2 := bytesize.New(4096)
sum := b1 + b2
fmt.Printf("%s", sum)
// Output:
// 5.00KB
}
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".
Code:
Output: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
}
1.00TB
3.00PB
3 petabytes
func (ByteSize) Format ¶
Returns a string representation of b with the specified formatting and units.
Demonstrates using different output formatting and units.
Code:
Output:Example¶
{
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
}
1.00TB
0.00097656 petabytes
func (*ByteSize) Get ¶
func (b *ByteSize) Get() interface{}
Satisfy the flag package Getter interface.
func (*ByteSize) Set ¶
Satisfy the flag package Value interface.
func (ByteSize) String ¶
String returns the string form of b using the package global Format and LongUnits options.
func (*ByteSize) Type ¶
Satisfy the pflag package Value interface.
func (*ByteSize) UnmarshalText ¶
Satisfy the encoding.TextUnmarshaler interface.
Source Files ¶
- Version
- v0.0.0-20220417184213-4913239db9cf (latest)
- Published
- Apr 17, 2022
- Platform
- js/wasm
- Imports
- 5 packages
- Last checked
- now –
Tools for package owners.