units – github.com/tonistiigi/units Index | Files

package units

import "github.com/tonistiigi/units"

Simple byte size formatting.

This package implements types that can be used in stdlib formatting functions like `fmt.Printf` to control the output of the expected printed string.

Floating point flags %f and %g print the value in using the correct unit suffix. Decimal units are default, # switches to binary units. If a value is best represented as full bytes, integer bytes are printed instead.

Examples:
	fmt.Printf("%.2f", 123 * B)   => "123B"
	fmt.Printf("%.2f", 1234 * B)  => "1.23kB"
	fmt.Printf("%g", 1200 * B)    => "1.2kB"
	fmt.Printf("%#g", 1024 * B)   => "1KiB"

Integer flag %d always prints the value in bytes. # flag adds an unit prefix.

Examples:
	fmt.Printf("%d", 1234 * B)    => "1234"
	fmt.Printf("%#d", 1234 * B)   => "1234B"

%v is equal to %g

Index

Constants

const (
	B Bytes = 1 << (10 * iota)
	KiB
	MiB
	GiB
	TiB
	PiB
	EiB

	KB = 1e3 * B
	MB = 1e3 * KB
	GB = 1e3 * MB
	TB = 1e3 * GB
	PB = 1e3 * TB
	EB = 1e3 * PB
)

Types

type Bytes

type Bytes int64

func (Bytes) Format

func (b Bytes) Format(f fmt.State, c rune)

Source Files

bytes.go

Version
v0.0.0-20180711220420-6950e57a87ea (latest)
Published
Jul 11, 2018
Platform
linux/amd64
Imports
4 packages
Last checked
1 year ago

Tools for package owners.