go-bytesize – github.com/inhies/go-bytesize Index | Examples | Files

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

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

const (
	B  ByteSize = 1
	KB ByteSize = 1 << (10 * iota)
	MB
	GB
	TB
	PB
	EB
)

Byte size size suffixes.

func New

func New(s float64) ByteSize

New returns a new ByteSize type set to s.

Example

Code:

{
	b := bytesize.New(1024)
	fmt.Printf("%s", b)

	// Output:
	// 1.00KB
}

Output:

1.00KB
Example (Math)

Code:

{
	b1 := bytesize.New(1024)
	b2 := bytesize.New(4096)
	sum := b1 + b2
	fmt.Printf("%s", sum)

	// Output:
	// 5.00KB
}

Output:

5.00KB

func Parse

func Parse(s string) (ByteSize, error)

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

Code:

{
	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
}

Output:

1.00TB
3.00PB
3 petabytes

func (ByteSize) Format

func (b ByteSize) Format(format string, unit string, longUnits bool) string

Returns a string representation of b with the specified formatting and units.

Example

Demonstrates using different output formatting and units.

Code:

{
	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
}

Output:

1.00TB
0.00097656 petabytes

func (*ByteSize) Get

func (b *ByteSize) Get() interface{}

Satisfy the flag package Getter interface.

func (*ByteSize) Set

func (b *ByteSize) Set(s string) error

Satisfy the flag package Value interface.

func (ByteSize) String

func (b ByteSize) String() string

String returns the string form of b using the package global Format and LongUnits options.

func (*ByteSize) Type

func (b *ByteSize) Type() string

Satisfy the pflag package Value interface.

func (*ByteSize) UnmarshalText

func (b *ByteSize) UnmarshalText(text []byte) error

Satisfy the encoding.TextUnmarshaler interface.

Source Files

bytesize.go

Version
v0.0.0-20220417184213-4913239db9cf (latest)
Published
Apr 17, 2022
Platform
js/wasm
Imports
5 packages
Last checked
now

Tools for package owners.