infounit

package module
v1.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2021 License: MIT Imports: 11 Imported by: 4

README

go-infounit

Go Reference MIT License

Overview

go-infounit is a Go package providing three data types for units of information:

  • ByteCount - number of bytes with both SI and binary prefixes
  • BitCount - number of bits with both SI and binary prefixes
  • BitRate - number of bits transferred or processed per unit of time

These values can be converted to human-readable string representations using the standard fmt.Printf family functions:

import (
	"fmt"
	"github.com/tunabay/go-infounit"
)

func main() {
	size := infounit.Megabyte * 2500
	fmt.Printf("% s\n", size)    // "2.5 GB"
	fmt.Printf("% .1S\n", size)  // "2.3 GiB"
	fmt.Printf("%# .2s\n", size) // "2.50 gigabytes"
	fmt.Printf("%# .3S\n", size) // "2.328 gibibytes"
	fmt.Printf("%d\n", size)     // "2500000000"

	bits := infounit.Kilobit * 32
	fmt.Printf("% s\n", bits)    // "32 kbit"
	fmt.Printf("% S\n", bits)    // "31.25 Kibit"
	fmt.Printf("%# .2s\n", bits) // "32.00 kilobits"
	fmt.Printf("%# .3S\n", bits) // "31.250 kibibits"
	fmt.Printf("%d\n", bits)     // "32000"

	rate := infounit.GigabitPerSecond * 123.45
	fmt.Printf("% s\n", rate)    // "123.45 Gbit/s"
	fmt.Printf("% .3S\n", rate)  // "114.972 Gibit/s"
	fmt.Printf("%# .1s\n", rate) // "123.5 gigabits per second"
	fmt.Printf("%# .2S\n", rate) // "114.97 gibibits per second"
	fmt.Printf("% .1a\n", rate)  // "123.5 Gbps"
	fmt.Printf("% .2A\n", rate)  // "114.97 Gibps"
}

Run in Go Playground

  • %s is for SI prefixes and %S is for binary prefixes.
  • %a and %A for BitRate use the non-standard abbreviation "bps".
  • ' '(space) flag puts a space between digits and the unit suffix.
  • '#' flag uses long unit suffixes.

They also implement convenience methods for:

  • Rounding to specified precision.
  • Scanning from human-readable string representation using fmt.Scanf family functions.

For more examples:

See also

License

go-infounit is available under the MIT license. See the LICENSE file for more information.

Documentation

Overview

Package infounit provides information unit data types that can be formatted into human-readable string representations. The following three data types are implemented:

ByteCount  non-negative number of bytes
BitCount   non-negative number of bits
BitRate    number of bits per unit of time

These types can be formatted into and scanned from human-readable string representations with both SI and binary prefixes using the standard Printf and Scanf family functions in the package fmt.

import "github.com/tunabay/go-infounit"

size := infounit.Megabyte * 2500
fmt.Printf("% s\n", size)     // "2.5 GB"
fmt.Printf("% .1S\n", size)   // "2.3 GiB"
fmt.Printf("%# .2s\n", size)  // "2.50 gigabytes"
fmt.Printf("%# .3S\n", size)  // "2.328 gibibytes"
fmt.Printf("%d\n", size)      // "2500000000"

bits := infounit.Kilobit * 32
fmt.Printf("% s\n", bits)     // "32 kbit"
fmt.Printf("% S\n", bits)     // "31.25 Kibit"
fmt.Printf("%# .2s\n", bits)  // "32.00 kilobits"
fmt.Printf("%# .3S\n", bits)  // "31.250 kibibits"
fmt.Printf("%d\n", bits)      // "32000"

rate := infounit.GigabitPerSecond * 123.45
fmt.Printf("% s\n", rate)     // "123.45 Gbit/s"
fmt.Printf("% .3S\n", rate)   // "114.972 Gibit/s"
fmt.Printf("%# .1s\n", rate)  // "123.5 gigabits per second"
fmt.Printf("%# .2S\n", rate)  // "114.97 gibibits per second"
fmt.Printf("% .1a\n", rate)   // "123.5 Gbps"
fmt.Printf("% .2A\n", rate)   // "114.97 Gibps"

Each data type also provides methods such as scanning and unit conversion.

ByteCount

ByteCount represents a number of bytes. It is a suitable type for storing file size, transferred data amount, etc. It is internally uint64. Thus new values can be declared in the usual ways and it is possible to convert between ByteCount and uint64 values.

var x infounit.ByteCount      // zero value is 0 bytes
y := infounit.ByteCount(200)  // declared with value 200 bytes
p := uint64(y)                // convert to uint64 with value 200

The common values for both SI and binary prefixes are defined as constants. Values with units can be declared by multiplying by them in a similar way to the Duration in the package time:

infounit.ByteCount(30) * infounit.Kilobyte  // 30 kB
infounit.ByteCount(50) * infounit.Kibibyte  // 50 KiB
infounit.ByteCount(70) * infounit.Terabyte  // 70 TB
infounit.ByteCount(90) * infounit.Pebibyte  // 90 PiB

Unit conversion is also possible by division:

int(infounit.Mebibyte / infounit.Kibibyte)  // 1024

See the Constants section below for the complete list of defined constants.

ByteCount values can be flexibly formatted using the standard Printf family functions in the package fmt, fmt.Printf, fmt.Fprintf, fmt.Sprintf, fmt.Errorf, and functions deriverd from them.

For ByteCount type, two custom 'verbs' are implemented:

%s	human readable format with SI prefix
%S	human readable format with binary prefix

By using these, the same value can be formatted with both SI and binary prefixes:

x := infounit.Kilobyte * 100
fmt.Printf("%.1s", x)        // 100.0kB
fmt.Printf("%.1S", x)        //  97.7KiB
y := infounit.Mebibyte * 100
fmt.Printf("%.1s", y)        // 102.4MB
fmt.Printf("%.1S", y)        // 100.0MiB

They also support flags ' '(space) and '#', width and precision, so the format can be changed flexibly:

z := infounit.ByteCount(987654321)
fmt.Printf("%.1s",  z)       // 987.7MB
fmt.Printf("% .1s", z)       // 987.7 MB
fmt.Printf("%# .1s", z)      // 987.7 megabytes
fmt.Printf("%.2S", z)        // 941.90MiB
fmt.Printf("%# .3S", z)      // 941.901 mebibytes

A ' '(space) flag puts a spece between digits and the unit. A '#' flag uses an alternative long unit name. See the Format method documentation bellow for details on all supported verbs and flags.

ByteCount type also supports the standard Scanf family functions in the package fmt, fmt.Scanf, fmt.Fscanf(), and fmt.Sscanf(). Human-readable string representations with SI and binary prefixes can be "scanned" as ByteCount values.

var file string
var size infounit.ByteCount
_, _ = fmt.Sscanf("%s is %s", "test.png is 5 kB", &file, &size)
fmt.Println(file, size)  // test.png 5.0 kB

Note that unlike Printf, the %s verb properly scans units with both SI and binary prefixes. So it is usually fine to use only the %s verb to scan. The %S verb treats the SI prefix representation as binary prefixes. See the Scan method documentation bellow for details.

BitCount

BitCount represents a number of bits. It is internally uint64. Functions and usage are almost the same as ByteCount, except that it represents a number of bits instead of bytes.

BitRate

BitRate represents a number of bits that are transferred or processed per unit of time. It is a suitable type for storing data transfer speed, processing speed, etc. It is internally float64.

Example (Printf)
size := infounit.Megabyte * 2500
fmt.Printf("% s\n", size)    // "2.5 GB"
fmt.Printf("% .1S\n", size)  // "2.3 GiB"
fmt.Printf("%# .2s\n", size) // "2.50 gigabytes"
fmt.Printf("%# .3S\n", size) // "2.328 gibibytes"
fmt.Printf("%d\n", size)     // "2500000000"

bits := infounit.Kilobit * 32
fmt.Printf("% s\n", bits)    // "32 kbit"
fmt.Printf("% S\n", bits)    // "31.25 Kibit"
fmt.Printf("%# .2s\n", bits) // "32.00 kilobits"
fmt.Printf("%# .3S\n", bits) // "31.250 kibibits"
fmt.Printf("%d\n", bits)     // "32000"

rate := infounit.GigabitPerSecond * 123.45
fmt.Printf("% s\n", rate)    // "123.45 Gbit/s"
fmt.Printf("% .3S\n", rate)  // "114.972 Gibit/s"
fmt.Printf("%# .1s\n", rate) // "123.5 gigabits per second"
fmt.Printf("%# .2S\n", rate) // "114.97 gibibits per second"
fmt.Printf("% .1a\n", rate)  // "123.5 Gbps"
fmt.Printf("% .2A\n", rate)  // "114.97 Gibps"
Output:

2.5 GB
2.3 GiB
2.50 gigabytes
2.328 gibibytes
2500000000
32 kbit
31.25 Kibit
32.00 kilobits
31.250 kibibits
32000
123.45 Gbit/s
114.972 Gibit/s
123.5 gigabits per second
114.97 gibibits per second
123.5 Gbps
114.97 Gibps

Index

Examples

Constants

View Source
const (
	Bit     BitCount = 1              // bit
	Kilobit          = 1000 * Bit     // kbit, kilobit
	Megabit          = 1000 * Kilobit // Mbit, megabit
	Gigabit          = 1000 * Megabit // Gbit, gigabit
	Terabit          = 1000 * Gigabit // Tbit, terabit
	Petabit          = 1000 * Terabit // Pbit, petabit
	Exabit           = 1000 * Petabit // Ebit, exabit
	Kibibit          = 1024 * Bit     // KiB, kibibit
	Mebibit          = 1024 * Kibibit // MiB, mebibit
	Gibibit          = 1024 * Mebibit // GiB, gibibit
	Tebibit          = 1024 * Gibibit // TiB, tebibit
	Pebibit          = 1024 * Tebibit // PiB, pebibit
	Exbibit          = 1024 * Pebibit // EiB, exbibit
)

Common BitCount values for units with SI and binary prefixes. To convert an integer of specific unit to a BitCount, multiply:

gbits := 100
fmt.Print(infounit.BitCount(gbits) * infounit.Gigabit)
View Source
const (
	BitPerSecond     BitRate = 1                       // bit/s, bit per second
	KilobitPerSecond         = 1000 * BitPerSecond     // kbit/s, kilobit per second
	MegabitPerSecond         = 1000 * KilobitPerSecond // Mbit/s, megabit per second
	GigabitPerSecond         = 1000 * MegabitPerSecond // Gbit/s, gigabit per second
	TerabitPerSecond         = 1000 * GigabitPerSecond // Tbit/s, terabit per second
	PetabitPerSecond         = 1000 * TerabitPerSecond // Pbit/s, petabit per second
	ExabitPerSecond          = 1000 * PetabitPerSecond // Ebit/s, exabit per second
	KibibitPerSecond         = 1024 * BitPerSecond     // Kibit/s, kibibit per second
	MebibitPerSecond         = 1024 * KibibitPerSecond // Mibit/s, mebibit per second
	GibibitPerSecond         = 1024 * MebibitPerSecond // Gibit/s, gibibit per second
	TebibitPerSecond         = 1024 * GibibitPerSecond // Tibit/s, tebibit per second
	PebibitPerSecond         = 1024 * TebibitPerSecond // Pibit/s, pebibit per second
	ExbibitPerSecond         = 1024 * PebibitPerSecond // Eibit/s, exbibit per second
)

Common BitRate values for units with SI and binary prefixes. To convert a float value of specific unit to a BitRate, multiply:

gbps := 100
fmt.Print(infounit.BitRate(gbps) * infounit.GigabitPerSecond)
View Source
const (
	Byte     ByteCount = 1               // B, byte
	Kilobyte           = 1000 * Byte     // kB, kilobyte
	Megabyte           = 1000 * Kilobyte // MB, megabyte
	Gigabyte           = 1000 * Megabyte // GB, gigabyte
	Terabyte           = 1000 * Gigabyte // TB, terabyte
	Petabyte           = 1000 * Terabyte // PB, petabyte
	Exabyte            = 1000 * Petabyte // EB, exabyte
	Kibibyte           = 1024 * Byte     // KiB, kibibyte
	Mebibyte           = 1024 * Kibibyte // MiB, mebibyte
	Gibibyte           = 1024 * Mebibyte // GiB, gibibyte
	Tebibyte           = 1024 * Gibibyte // TiB, tebibyte
	Pebibyte           = 1024 * Tebibyte // PiB, pebibyte
	Exbibyte           = 1024 * Pebibyte // EiB, exbibyte
)

Common ByteCount values for units with SI and binary prefixes. To convert an integer of specific unit to a ByteCount, multiply:

gb := 100
fmt.Print(infounit.ByteCount(gb) * infounit.Gigabyte)

Variables

View Source
var ErrDivZeroBitRate = errors.New("division by zero bit rate")

ErrDivZeroBitRate is the error thrown when trying to divide by zero bit rate.

View Source
var ErrMalformedRepresentation = errors.New("malformed representation")

ErrMalformedRepresentation is the error thrown when trying to conver a malformed string representation.

View Source
var ErrOutOfRange = errors.New("out of range")

ErrOutOfRange is the error thrown when the result exceeds the range.

Functions

func AtomicStoreBitCount

func AtomicStoreBitCount(addr *BitCount, val BitCount)

AtomicStoreBitCount atomically stores val into *addr. A wrapper function for the package sync/atomic.

func AtomicStoreBitRate

func AtomicStoreBitRate(addr *BitRate, val BitRate)

AtomicStoreBitRate atomically stores val into *addr. A wrapper function for the package sync/atomic.

func AtomicStoreByteCount

func AtomicStoreByteCount(addr *ByteCount, val ByteCount)

AtomicStoreByteCount atomically stores val into *addr. A wrapper function for the package sync/atomic.

Types

type BitCount

type BitCount uint64

BitCount represents a non-negative bit count. BitCount values can be converted to human-readable string representations by the standard Printf family functions in the package fmt. See the documentation of Format method bellow for details.

Range: 0 bits through 18446744073709551615 bits (=2 EiB)

func AtomicAddBitCount

func AtomicAddBitCount(addr *BitCount, delta BitCount) BitCount

AtomicAddBitCount atomically adds delta to *addr and returns the new value. A wrapper function for the package sync/atomic.

func AtomicLoadBitCount

func AtomicLoadBitCount(addr *BitCount) BitCount

AtomicLoadBitCount atomically loads *addr. A wrapper function for the package sync/atomic.

func AtomicSubBitCount

func AtomicSubBitCount(addr *BitCount, delta BitCount) BitCount

AtomicSubBitCount atomically subtract delta from *addr and returns the new value. A wrapper function for the package sync/atomic.

func AtomicSwapBitCount

func AtomicSwapBitCount(addr *BitCount, val BitCount) BitCount

AtomicSwapBitCount atomically stores val into *addr and returns the previous *addr value. A wrapper function for the package sync/atomic.

func ParseBitCount added in v1.1.0

func ParseBitCount(s string) (BitCount, error)

ParseBitCount converts a human-readable string representation into a BitCount value. The human-readable string is a decimal number with a unit suffix. SI and binary prefixes are correctly recognized.

func ParseBitCountBinary added in v1.1.0

func ParseBitCountBinary(s string) (BitCount, error)

ParseBitCountBinary is the same as ParseBitCount except that it treats the SI prefixes as binary prefixes. That is, it parses "100 kbit" as 100 Kibit (=102400 bit).

func (BitCount) ByteCount

func (bc BitCount) ByteCount() (ByteCount, BitCount)

ByteCount returns the value converted to the number of bytes and the number of remaining bits.

func (BitCount) CalcBitRate added in v1.0.0

func (bc BitCount) CalcBitRate(duration time.Duration) BitRate

CalcBitRate calculates the bit rate when the number of bits is transferred or processed in the specified duration.

func (BitCount) CalcTime added in v1.0.0

func (bc BitCount) CalcTime(rate BitRate) (time.Duration, error)

CalcTime calculates the duration it takes to transfer or process the number of bits at the specified rate.

func (BitCount) Convert

func (bc BitCount) Convert(unit BitCount) float64

Convert converts the bit count to a float value in the specified unit. If the goal is to output or to create a string in a human-readable format, fmt.Printf or fmt.Sprintf are preferred.

Example (BinaryPrefix)
x := infounit.BitCount(123456789)

fmt.Printf("%f\n", x.Convert(infounit.Kibibit))
fmt.Printf("%f\n", x.Convert(infounit.Mebibit))
fmt.Printf("%f\n", x.Convert(infounit.Gibibit))
fmt.Printf("%f\n", x.Convert(infounit.Tebibit))
Output:

120563.270508
117.737569
0.114978
0.000112
Example (SiPrefix)
x := infounit.BitCount(123456789)

fmt.Printf("%f\n", x.Convert(infounit.Kilobit))
fmt.Printf("%f\n", x.Convert(infounit.Megabit))
fmt.Printf("%f\n", x.Convert(infounit.Gigabit))
fmt.Printf("%f\n", x.Convert(infounit.Terabit))
Output:

123456.789000
123.456789
0.123457
0.000123

func (BitCount) ConvertRound

func (bc BitCount) ConvertRound(unit BitCount, precision int) float64

ConvertRound is the same as Convert except that it returns a value rounded to the specified precision. If the goal is to output or to create a string in a human-readable format, fmt.Printf or fmt.Sprintf is preferred.

Example (BinaryPrefix)
x := infounit.BitCount(123456789)

fmt.Printf("%f\n", x.ConvertRound(infounit.Kibibit, 2))
fmt.Printf("%f\n", x.ConvertRound(infounit.Kibibit, 5))
fmt.Printf("%f\n", x.ConvertRound(infounit.Mebibit, 0))
fmt.Printf("%f\n", x.ConvertRound(infounit.Mebibit, 3))
fmt.Printf("%f\n", x.ConvertRound(infounit.Tebibit, 6))
Output:

120563.270000
120563.270510
118.000000
117.738000
0.000112
Example (SiPrefix)
x := infounit.BitCount(123456789)

fmt.Printf("%f\n", x.ConvertRound(infounit.Kilobit, 2))
fmt.Printf("%f\n", x.ConvertRound(infounit.Kilobit, 5))
fmt.Printf("%f\n", x.ConvertRound(infounit.Megabit, 0))
fmt.Printf("%f\n", x.ConvertRound(infounit.Megabit, 3))
fmt.Printf("%f\n", x.ConvertRound(infounit.Terabit, 6))
Output:

123456.790000
123456.789000
123.000000
123.457000
0.000123

func (BitCount) Format

func (bc BitCount) Format(s fmt.State, verb rune)

Format implements the Formatter interface in the package fmt to format BitCount values. This gives the ability to format BitCount values in human-readable format using standard Printf family functions in the package fmt; fmt.Printf, fmt.Fprintf, fmt.Sprintf, fmt.Errorf, and functions derived from them.

For BitCount type, two custom 'verbs' are implemented:

%s	human-readable format with SI prefix
%S	human-readable format with binary prefix

Width and precision can be specified to both %s and %S:

%s	default width, default precision
%7s	width 7, default precision
%.2s	default width, precision 2
%7.2s	width 7, precision 2
%7.s	width 7, precision 0

Regardless of the precision specified, while the unit is bit, no decimal parts are printed.

The following flags are also available for both %s and %S:

' '	(space) print a space between digits and unit; e.g. "12.3 kbit"
#	use long unit name; e.g. "kilobit", "mebibit"
-	pad with spaces on the right rather than the left (left-justify)
0	pad with leading zeros rather than spaces

%v prints in the default format:

%v	default format, same as "% .1s"
%#v	GoString(); e.g. "BitCount(1024)"

The following uint64 compatible verbs are also supported. They print the integer values always in bit:

%b	base 2
%d	base 10
%o	base 8
%x	base 16, with lower-case letters for a-f
%X	base 16, with upper-case letters for A-F

See the package fmt documentation for details.

Example (Errorf)
limit := infounit.Kilobit * 100

check := func(size infounit.BitCount) error {
	if limit < size {
		return fmt.Errorf("too large: %v", size)
	}
	return nil
}

for i := 0; i < 8; i++ {
	size := infounit.BitCount(1 << (6 * i))
	if err := check(size); err != nil {
		fmt.Println("error:", err)
		continue
	}
	fmt.Println("processed:", size)
}
Output:

processed: 1 bit
processed: 64 bit
processed: 4.1 kbit
error: too large: 262.1 kbit
error: too large: 16.8 Mbit
error: too large: 1.1 Gbit
error: too large: 68.7 Gbit
error: too large: 4.4 Tbit
Example (Printf)
x := infounit.Kilobit * 100 // 100000 bytes

// Default format
fmt.Printf("%v\n", x)  // default format, same as "% .1s"
fmt.Printf("%#v\n", x) // Go syntax format

// SI prefix
fmt.Printf("%s\n", x)      // default precision
fmt.Printf("% s\n", x)     // with space
fmt.Printf("%012.2s\n", x) // zero padding, width 12, precision 2
fmt.Printf("%#.2s\n", x)   // long unit
fmt.Printf("%# .2s\n", x)  // long unit, with space
fmt.Printf("[%12s]\n", x)  // width 12
fmt.Printf("[%-12s]\n", x) // width 12, left-aligned

// Binary prefix
fmt.Printf("%S\n", x)     // default precision
fmt.Printf("% S\n", x)    // with space
fmt.Printf("%.1S\n", x)   // precision 1
fmt.Printf("%# .2S\n", x) // precision 2, long unit, with space

// Integer
fmt.Printf("%d\n", x) // decimal in bytes
fmt.Printf("%x\n", x) // hexadecimal in bytes
Output:

100.0 kbit
BitCount(100000)
100kbit
100 kbit
00100.00kbit
100.00kilobits
100.00 kilobits
[     100kbit]
[100kbit     ]
97.65625Kibit
97.65625 Kibit
97.7Kibit
97.66 kibibits
100000
186a0

func (BitCount) GoString

func (bc BitCount) GoString() string

GoString returns a string representation of the BitCount value in Go syntax format. This implements the GoStringer interface in the package fmt.

func (BitCount) IsZero added in v1.1.1

func (bc BitCount) IsZero() bool

IsZero returns whether the BitCount value is zero.

func (*BitCount) MarshalBinary

func (bc *BitCount) MarshalBinary() ([]byte, error)

MarshalBinary encodes the BitCount value into a binary form and returns the result. This implements the BinaryMarshaler interface in the package encoding.

func (*BitCount) MarshalJSON added in v1.1.2

func (bc *BitCount) MarshalJSON() ([]byte, error)

MarshalJSON encodes the BitCount value into a string for a JSON field.

func (*BitCount) MarshalText

func (bc *BitCount) MarshalText() ([]byte, error)

MarshalText encodes the BitCount value into a UTF-8-encoded text and returns the result. This implements the TextMarshaler interface in the package encoding.

func (*BitCount) MarshalYAML added in v1.1.1

func (bc *BitCount) MarshalYAML() (interface{}, error)

MarshalYAML encodes the BitCount value into a uint64 for a YAML field.

func (*BitCount) Scan

func (bc *BitCount) Scan(state fmt.ScanState, verb rune) error

Scan implements the Scanner interface in the package fmt to scan BitCount values from strings. This allows BitCount values to be scanned from human-readable string representations with unit suffixes using the standard Scanf family functions in the package fmt; fmt.Scanf, fmt.Fscanf, and fmt.Sscanf.

For BitCount type, four custom 'verbs' are implemented:

%s, %u	human-readable formats with both SI and binary prefixes
%S, %U	treat SI prefix as binary prefix; 1 kilobits = 1024 bits

Note that, unlike Format, the %s verb can properly scan expressions with units using both SI and binary prefixes.

Therefore, it is usually recommended to scan using only the %s verb. The %S verb is the same as %s, except that it treats the SI prefix as binary prefix. That is, %S scans the expression "100 kbit" as 100 Kibit (=102400 bit).

For verbs %s and %S, unit suffix is mandatory. If the first token consists only of digits, it is assumed that the next token is a unit suffix, with one space in between. On the other hand, %u and %U do not allow expressions with a space between digits and the unit suffix. They always scan only one token. They assume that if the token consists only of digits, it is the number of bits.

The following verbs are compatible with uint64 and scans integers without a unit suffix. If it is clear that there is absolutely no unit suffix in the input, the use of these is recommended:

%b	base 2
%o	base 8
%d	base 10
%x, %X	base 16

See the package fmt documentation for details.

Example (Sscanf)
src := []string{
	"kawaii.png is 1.23Mbit",
	"work.xlsx is 234.56 kibibits",
	"huge.zip is 999 Tbit",
}
for _, s := range src {
	var file string
	var size infounit.BitCount
	_, _ = fmt.Sscanf(s, "%s is %s", &file, &size)
	fmt.Println(file, size)
}
Output:

kawaii.png 1.2 Mbit
work.xlsx 240.2 kbit
huge.zip 999.0 Tbit

func (BitCount) String

func (bc BitCount) String() string

String returns the human-readable string representing the bit count using SI prefix. This implements the Stringer interface in the package fmt.

func (*BitCount) UnmarshalBinary

func (bc *BitCount) UnmarshalBinary(data []byte) error

UnmarshalBinary decodes the BitCount value from a binary form. This implements the BinaryUnmarshaler interface in the package encoding.

func (*BitCount) UnmarshalJSON added in v1.1.2

func (bc *BitCount) UnmarshalJSON(b []byte) error

UnmarshalJSON decodes the BitCount value from a JSON field.

func (*BitCount) UnmarshalText

func (bc *BitCount) UnmarshalText(text []byte) error

UnmarshalText decodes the BitCount value from a UTF-8-encoded text form. This implements the TextUnmarshaler interface in the package encoding.

func (*BitCount) UnmarshalYAML added in v1.1.1

func (bc *BitCount) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML decodes the BitCount value from a YAML field.

type BitRate

type BitRate float64

BitRate represents a number of bits that are transferred or processed per unit of time. BitRate values can be converted to human-readable string representations by the standard Printf family functions in the package fmt. See the documentation of Format method bellow for details.

func AtomicLoadBitRate

func AtomicLoadBitRate(addr *BitRate) BitRate

AtomicLoadBitRate atomically loads *addr. A wrapper function for the package sync/atomic.

func AtomicSwapBitRate

func AtomicSwapBitRate(addr *BitRate, val BitRate) BitRate

AtomicSwapBitRate atomically stores val into *addr and returns the previous *addr value. A wrapper function for the package sync/atomic.

func ParseBitRate added in v1.1.0

func ParseBitRate(s string) (BitRate, error)

ParseBitRate converts a human-readable string representation into a BitRate value. The human-readable string is a decimal number with a unit suffix. SI and binary prefixes are correctly recognized.

func ParseBitRateBinary added in v1.1.0

func ParseBitRateBinary(s string) (BitRate, error)

ParseBitRateBinary is the same as ParseBitRate except that it treats the SI prefixes as binary prefixes. That is, it parses "100 kbit/s" as 100 Kibit/s (=102400 bit/s).

func (BitRate) CalcBitCount added in v1.0.0

func (br BitRate) CalcBitCount(duration time.Duration) (BitCount, error)

CalcBitCount calculates the number of bits that can be transferred or processed in the specified duration at this bit rate.

func (BitRate) CalcByteCount added in v1.0.0

func (br BitRate) CalcByteCount(duration time.Duration) (ByteCount, error)

CalcByteCount calculates the number of bytes that can be transferred or processed in the specified duration at this bit rate.

func (BitRate) Convert

func (br BitRate) Convert(unit BitRate) float64

Convert converts the bit rate to a float value in the specified unit. If the goal is to output or to create a string in a human-readable format, fmt.Printf or fmt.Sprintf is preferred.

Example (BinaryPrefix)
x := infounit.BitRate(123456789.0)

fmt.Printf("%f\n", x.Convert(infounit.KibibitPerSecond))
fmt.Printf("%f\n", x.Convert(infounit.MebibitPerSecond))
fmt.Printf("%f\n", x.Convert(infounit.GibibitPerSecond))
fmt.Printf("%f\n", x.Convert(infounit.TebibitPerSecond))
Output:

120563.270508
117.737569
0.114978
0.000112
Example (SiPrefix)
x := infounit.BitRate(123456789.0)

fmt.Printf("%f\n", x.Convert(infounit.KilobitPerSecond))
fmt.Printf("%f\n", x.Convert(infounit.MegabitPerSecond))
fmt.Printf("%f\n", x.Convert(infounit.GigabitPerSecond))
fmt.Printf("%f\n", x.Convert(infounit.TerabitPerSecond))
Output:

123456.789000
123.456789
0.123457
0.000123

func (BitRate) ConvertRound

func (br BitRate) ConvertRound(unit BitRate, precision int) float64

ConvertRound is the same as Convert except that it returns a value rounded to the specified precision. If the goal is to output or to create a string in a human-readable format, fmt.Printf or fmt.Sprintf is preferred.

Example (BinaryPrefix)
x := infounit.BitRate(123456789.0)

fmt.Printf("%f\n", x.ConvertRound(infounit.KibibitPerSecond, 2))
fmt.Printf("%f\n", x.ConvertRound(infounit.KibibitPerSecond, 5))
fmt.Printf("%f\n", x.ConvertRound(infounit.MebibitPerSecond, 0))
fmt.Printf("%f\n", x.ConvertRound(infounit.MebibitPerSecond, 3))
fmt.Printf("%f\n", x.ConvertRound(infounit.TebibitPerSecond, 6))
Output:

120563.270000
120563.270510
118.000000
117.738000
0.000112
Example (SiPrefix)
x := infounit.BitRate(123456789.0)

fmt.Printf("%f\n", x.ConvertRound(infounit.KilobitPerSecond, 2))
fmt.Printf("%f\n", x.ConvertRound(infounit.KilobitPerSecond, 5))
fmt.Printf("%f\n", x.ConvertRound(infounit.MegabitPerSecond, 0))
fmt.Printf("%f\n", x.ConvertRound(infounit.MegabitPerSecond, 3))
fmt.Printf("%f\n", x.ConvertRound(infounit.TerabitPerSecond, 6))
Output:

123456.790000
123456.789000
123.000000
123.457000
0.000123

func (BitRate) Format

func (br BitRate) Format(s fmt.State, verb rune)

Format implements the Formatter interface in the package fmt to format BitRate values. This gives the ability to format the BitRate values in human-readable format using standard Printf family functions in the package fmt; fmt.Printf, fmt.Fprintf, fmt.Sprintf, fmt.Errorf, and functions derived from them.

For ByteRate type, four custom 'verbs' are implemented:

%s, %a	human-readable format with SI prefix
%S, %A	human-readable format with binary prefix

%s and %S use "bit/s" unit suffix; e.g. "Mbit/s", "Gibit/s" %a and %A use "bps" unit suffix; e.g. "Mbps", "Gibps"

Width and precision can be specified to all of %s, %S, %a and %A:

%s	default width, default precision
%7s	width 7, default precision
%.2s	default width, precision 2
%7.2s	width 7, precision 2
%7.s	width 7, precision 0

The following flags are also available for %s, %S, %a and %A:

' '	(space) print a space between digits and unit; e.g. "12.3 Mbit/s"
#	use long unit name; e.g. "kilobits per second", "mebibits per second"
-	pad with spaces on the right rather than the left (left-justify)
0	pad with leading zeros rather than spaces

%v prints in the default format:

%v	default format, same as "% .1s"
%#v	GoString(); e.g. "BitRate(1234567.89)"

The following float64 compatible verbs are also supported. They print the float values always in bit/s:

%b	decimalless scientific notation, e.g. -123456p-78
%e	scientific notation, e.g. -1.234456e+78
%E	scientific notation, e.g. -1.234456E+78
%f	decimal point but no exponent, e.g. 123.456
%F	synonym for %f
%g	%e for large exponents, %f otherwise
%G	%E for large exponents, %F otherwise
%x	hexadecimal notation, e.g. -0x1.23abcp+20
%X	upper-case hexadecimal notation, e.g. -0X1.23ABCP+20

See the package fmt documentation for details.

Example (Errorf)
limit := infounit.KilobitPerSecond * 100

check := func(rate infounit.BitRate) error {
	if limit < rate {
		return fmt.Errorf("too fast: %v", rate)
	}
	return nil
}

for i := 0; i < 8; i++ {
	rate := infounit.BitRate(uint64(1 << (6 * i)))
	if err := check(rate); err != nil {
		fmt.Println("error:", err)
		continue
	}
	fmt.Println("rate:", rate)
}
Output:

rate: 1.0 bit/s
rate: 64.0 bit/s
rate: 4.1 kbit/s
error: too fast: 262.1 kbit/s
error: too fast: 16.8 Mbit/s
error: too fast: 1.1 Gbit/s
error: too fast: 68.7 Gbit/s
error: too fast: 4.4 Tbit/s
Example (Printf)
x := infounit.KilobitPerSecond * 123.456 // 123.456 kbit/s

// Default format
fmt.Printf("%v\n", x)  // default format, same as "% .1s"
fmt.Printf("%#v\n", x) // Go syntax format

// SI prefix
fmt.Printf("%s\n", x)      // default precision
fmt.Printf("% s\n", x)     // with space
fmt.Printf("%014.2s\n", x) // zero padding, width 14, precision 2
fmt.Printf("%#.2s\n", x)   // long unit
fmt.Printf("%# .2s\n", x)  // long unit, with space
fmt.Printf("[%16s]\n", x)  // width 16
fmt.Printf("[%-16s]\n", x) // width 16, left-aligned
fmt.Printf("% a\n", x)     // non-standard abbreviation "bps"

// Binary prefix
fmt.Printf("%S\n", x)     // default precision
fmt.Printf("% S\n", x)    // with space
fmt.Printf("%.1S\n", x)   // precision 1
fmt.Printf("%# .2S\n", x) // precision 2, long unit, with space
fmt.Printf("% A\n", x)    // non-standard abbreviation "bps"

// Float
fmt.Printf("%f\n", x) // floating point value in bit/s
Output:

123.5 kbit/s
BitRate(123456)
123.456kbit/s
123.456 kbit/s
00123.46kbit/s
123.46kilobits per second
123.46 kilobits per second
[   123.456kbit/s]
[123.456kbit/s   ]
123.456 kbps
120.5625Kibit/s
120.5625 Kibit/s
120.6Kibit/s
120.56 kibibits per second
120.5625 Kibps
123456.000000

func (BitRate) GoString

func (br BitRate) GoString() string

GoString returns a string representation of the BitRate value in Go syntax format. This implements the GoStringer interface in the package fmt.

func (BitRate) IsInf

func (br BitRate) IsInf(sign int) bool

IsInf reports whether the bit rate value is an infinity, according to sign. If sign > 0, IsInf reports whether the bit rate value is positive infinity. If sign < 0, IsInf reports whether the bit rate value is negative infinity. If sign == 0, IsInf reports whether the bit rate value is either infinity.

func (BitRate) IsNaN

func (br BitRate) IsNaN() bool

IsNaN reports whether the bit rate value is an IEEE 754 "not-a-number" value.

func (BitRate) IsZero added in v1.1.1

func (br BitRate) IsZero() bool

IsZero returns whether the BitRate value is zero.

func (*BitRate) MarshalBinary

func (br *BitRate) MarshalBinary() ([]byte, error)

MarshalBinary encodes the BitRate value into a binary form and returns the result. This implements the BinaryMarshaler interface in the package encoding.

func (*BitRate) MarshalJSON added in v1.1.2

func (br *BitRate) MarshalJSON() ([]byte, error)

MarshalJSON encodes the BitRate value into a string for a JSON field.

func (*BitRate) MarshalText

func (br *BitRate) MarshalText() ([]byte, error)

MarshalText encodes the BitRate value into a UTF-8-encoded text and returns the result. This implements the TextMarshaler interface in the package encoding.

func (*BitRate) MarshalYAML added in v1.1.1

func (br *BitRate) MarshalYAML() (interface{}, error)

MarshalYAML encodes the BitRate value into a float64 for a YAML field.

func (*BitRate) Scan

func (br *BitRate) Scan(state fmt.ScanState, verb rune) error

Scan implements the Scanner interface in the package fmt to scan BitRate values from strings. This allows BitRate values to be scanned from human-readable string representations with unit suffixes using the standard Scanf family functions in the package fmt; fmt.Scanf, fmt.Fscanf, and fmt.Sscanf().

For BitRate type, four custom 'verbs' are implemented:

%s, %u	human-readable formats with both SI and binary prefixes
%S, %U	treat SI prefix as binary prefix; 1 kbit/s = 1024 bit/s

Note that, unlike Format, the %s verb can properly scan expressions with units using both SI and binary prefixes.

Therefore, it is usually recommended to scan using only the %s verb. The %S verb is the same as %s, except that it treats the SI prefix as binary prefix. That is, %S scans the expression "100 kbit/s" as 100 Kibit/s (=102400 bit/s).

For verbs %s and %S, unit suffix is mandatory. If the first token consists only of digits, it is assumed that the next token is a unit suffix, with one space in between. On the other hand, %u and %U do not allow expressions with a space between digits and the unit suffix. They always scan only one token. They assume that if the token consists only of digits, it is the number of bit/s.

The following verbs are compatible with float64 and scans floating point values without a unit suffix. If it is clear that there is absolutely no unit suffix in the input, the use of these is recommended:

%f, %F	floating point representation

See the package fmt documentation for details.

func (BitRate) String

func (br BitRate) String() string

String returns the human-readable string representing the bit rate using SI prefix. This implements the Stringer interface in the package fmt.

func (*BitRate) UnmarshalBinary

func (br *BitRate) UnmarshalBinary(data []byte) error

UnmarshalBinary decodes the BitRate value from a binary form. This implements the BinaryUnmarshaler interface in the package encoding.

func (*BitRate) UnmarshalJSON added in v1.1.2

func (br *BitRate) UnmarshalJSON(b []byte) error

UnmarshalJSON decodes the BitRate value from a JSON field.

func (*BitRate) UnmarshalText

func (br *BitRate) UnmarshalText(text []byte) error

UnmarshalText decodes the BitRate value from a UTF-8-encoded text form. This implements the TextUnmarshaler interface in the package encoding.

func (*BitRate) UnmarshalYAML added in v1.1.1

func (br *BitRate) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML decodes the BitRate value from a YAML field.

type ByteCount

type ByteCount uint64

ByteCount represents a non-negative byte count. ByteCount values can be converted to human-readable string representations by the standard Printf family functions in the package fmt. See the documentation of Format method bellow for details.

Range: 0 bytes through 18446744073709551615bytes (=16 EiB)

func AtomicAddByteCount

func AtomicAddByteCount(addr *ByteCount, delta ByteCount) ByteCount

AtomicAddByteCount atomically adds delta to *addr and returns the new value. A wrapper function for the package sync/atomic.

func AtomicLoadByteCount

func AtomicLoadByteCount(addr *ByteCount) ByteCount

AtomicLoadByteCount atomically loads *addr. A wrapper function for the package sync/atomic.

func AtomicSubByteCount

func AtomicSubByteCount(addr *ByteCount, delta ByteCount) ByteCount

AtomicSubByteCount atomically subtract delta from *addr and returns the new value. A wrapper function for the package sync/atomic.

func AtomicSwapByteCount

func AtomicSwapByteCount(addr *ByteCount, val ByteCount) ByteCount

AtomicSwapByteCount atomically stores val into *addr and returns the previous *addr value. A wrapper function for the package sync/atomic.

func ParseByteCount added in v1.1.0

func ParseByteCount(s string) (ByteCount, error)

ParseByteCount converts a human-readable string representation into a ByteCount value. The human-readable string is a decimal number with a unit suffix. SI and binary prefixes are correctly recognized.

func ParseByteCountBinary added in v1.1.0

func ParseByteCountBinary(s string) (ByteCount, error)

ParseByteCountBinary is the same as ParseByteCount except that it treats the SI prefixes as binary prefixes. That is, it parses "100 kB" as 100 KiB (=102400 B).

func (ByteCount) BitCount

func (bc ByteCount) BitCount() (BitCount, error)

BitCount returns the value converted to the number of bits. If the number of bits is too large, an ErrOutOfRange will be returned.

func (ByteCount) CalcBitRate added in v1.0.0

func (bc ByteCount) CalcBitRate(duration time.Duration) BitRate

CalcBitRate calculates the bit rate when the number of bytes is transferred or processed in the specified duration.

func (ByteCount) CalcTime added in v1.0.0

func (bc ByteCount) CalcTime(rate BitRate) (time.Duration, error)

CalcTime calculates the duration it takes to transfer or process the number of bytes at the specified rate.

func (ByteCount) Convert

func (bc ByteCount) Convert(unit ByteCount) float64

Convert converts the byte count to a float value in the specified unit. If the goal is to output or to create a string in a human-readable format, fmt.Printf or fmt.Sprintf is preferred.

Example (BinaryPrefix)
x := infounit.ByteCount(123456789)

fmt.Printf("%f\n", x.Convert(infounit.Kibibyte))
fmt.Printf("%f\n", x.Convert(infounit.Mebibyte))
fmt.Printf("%f\n", x.Convert(infounit.Gibibyte))
fmt.Printf("%f\n", x.Convert(infounit.Tebibyte))
Output:

120563.270508
117.737569
0.114978
0.000112
Example (SiPrefix)
x := infounit.ByteCount(123456789)

fmt.Printf("%f\n", x.Convert(infounit.Kilobyte))
fmt.Printf("%f\n", x.Convert(infounit.Megabyte))
fmt.Printf("%f\n", x.Convert(infounit.Gigabyte))
fmt.Printf("%f\n", x.Convert(infounit.Terabyte))
Output:

123456.789000
123.456789
0.123457
0.000123

func (ByteCount) ConvertRound

func (bc ByteCount) ConvertRound(unit ByteCount, precision int) float64

ConvertRound is the same as Convert except that it returns a value rounded to the specified precision. If the goal is to output or to create a string in a human-readable format, fmt.Printf or fmt.Sprintf is preferred.

Example (BinaryPrefix)
x := infounit.ByteCount(123456789)

fmt.Printf("%f\n", x.ConvertRound(infounit.Kibibyte, 2))
fmt.Printf("%f\n", x.ConvertRound(infounit.Kibibyte, 5))
fmt.Printf("%f\n", x.ConvertRound(infounit.Mebibyte, 0))
fmt.Printf("%f\n", x.ConvertRound(infounit.Mebibyte, 3))
fmt.Printf("%f\n", x.ConvertRound(infounit.Tebibyte, 6))
Output:

120563.270000
120563.270510
118.000000
117.738000
0.000112
Example (SiPrefix)
x := infounit.ByteCount(123456789)

fmt.Printf("%f\n", x.ConvertRound(infounit.Kilobyte, 2))
fmt.Printf("%f\n", x.ConvertRound(infounit.Kilobyte, 5))
fmt.Printf("%f\n", x.ConvertRound(infounit.Megabyte, 0))
fmt.Printf("%f\n", x.ConvertRound(infounit.Megabyte, 3))
fmt.Printf("%f\n", x.ConvertRound(infounit.Terabyte, 6))
Output:

123456.790000
123456.789000
123.000000
123.457000
0.000123

func (ByteCount) Format

func (bc ByteCount) Format(s fmt.State, verb rune)

Format implements the Formatter interface in the package fmt to format ByteCount values. This gives the ability to format ByteCount values in human-readable format using standard Printf family functions in the package fmt; fmt.Printf, fmt.Fprintf, fmt.Sprintf, fmt.Errorf, and functions derived from them.

For ByteCount type, two custom 'verbs' are implemented:

%s	human-readable format with SI prefix
%S	human-readable format with binary prefix

Width and precision can be specified to both %s and %S:

%s	default width, default precision
%7s	width 7, default precision
%.2s	default width, precision 2
%7.2s	width 7, precision 2
%7.s	width 7, precision 0

Regardless of the precision specified, while the unit is byte, no decimal parts are printed.

The following flags are also available for both %s and %S:

' '	(space) print a space between digits and unit; e.g. "12.3 kB"
#	use long unit name; e.g. "kilobyte", "mebibyte"
-	pad with spaces on the right rather than the left (left-justify)
0	pad with leading zeros rather than spaces

%v prints in the default format:

%v	default format, same as "% .1s"
%#v	GoString(); e.g. "ByteCount(1024)"

The following uint64 compatible verbs are also supported. They print the integer values always in byte:

%b	base 2
%d	base 10
%o	base 8
%x	base 16, with lower-case letters for a-f
%X	base 16, with upper-case letters for A-F

See the package fmt documentation for details.

Example (Errorf)
limit := infounit.Kilobyte * 100

check := func(size infounit.ByteCount) error {
	if limit < size {
		return fmt.Errorf("too large: %v", size)
	}
	return nil
}

for i := 0; i < 8; i++ {
	size := infounit.ByteCount(1 << (6 * i))
	if err := check(size); err != nil {
		fmt.Println("error:", err)
		continue
	}
	fmt.Println("processed:", size)
}
Output:

processed: 1 B
processed: 64 B
processed: 4.1 kB
error: too large: 262.1 kB
error: too large: 16.8 MB
error: too large: 1.1 GB
error: too large: 68.7 GB
error: too large: 4.4 TB
Example (Printf)
x := infounit.Kilobyte * 100 // 100000 bytes

// Default format
fmt.Printf("%v\n", x)  // default format, same as "% .1s"
fmt.Printf("%#v\n", x) // Go syntax format

// SI prefix
fmt.Printf("%s\n", x)      // default precision
fmt.Printf("% s\n", x)     // with space
fmt.Printf("%010.2s\n", x) // zero padding, width 10, precision 2
fmt.Printf("%#.2s\n", x)   // long unit
fmt.Printf("%# .2s\n", x)  // long unit, with space
fmt.Printf("[%10s]\n", x)  // width 10
fmt.Printf("[%-10s]\n", x) // width 10, left-aligned

// Binary prefix
fmt.Printf("%S\n", x)     // default precision
fmt.Printf("% S\n", x)    // with space
fmt.Printf("%.1S\n", x)   // precision 1
fmt.Printf("%# .2S\n", x) // precision 2, long unit, with space

// Integer
fmt.Printf("%d\n", x) // decimal in bytes
fmt.Printf("%x\n", x) // hexadecimal in bytes
Output:

100.0 kB
ByteCount(100000)
100kB
100 kB
00100.00kB
100.00kilobytes
100.00 kilobytes
[     100kB]
[100kB     ]
97.65625KiB
97.65625 KiB
97.7KiB
97.66 kibibytes
100000
186a0

func (ByteCount) GoString

func (bc ByteCount) GoString() string

GoString returns a string representation of the ByteCount value in Go syntax format. This implements the GoStringer interface in the package fmt.

func (ByteCount) IsZero added in v1.1.1

func (bc ByteCount) IsZero() bool

IsZero returns whether the ByteCount value is zero.

func (*ByteCount) MarshalBinary

func (bc *ByteCount) MarshalBinary() ([]byte, error)

MarshalBinary encodes the ByteCount value into a binary form and returns the result. This implements the BinaryMarshaler interface in the package encoding.

func (*ByteCount) MarshalJSON added in v1.1.2

func (bc *ByteCount) MarshalJSON() ([]byte, error)

MarshalJSON encodes the ByteCount value into a string for a JSON field.

func (*ByteCount) MarshalText

func (bc *ByteCount) MarshalText() ([]byte, error)

MarshalText encodes the ByteCount value into a UTF-8-encoded text and returns the result. This implements the TextMarshaler interface in the package encoding.

func (*ByteCount) MarshalYAML added in v1.1.1

func (bc *ByteCount) MarshalYAML() (interface{}, error)

MarshalYAML encodes the ByteCount value into a string for a YAML field.

func (*ByteCount) Scan

func (bc *ByteCount) Scan(state fmt.ScanState, verb rune) error

Scan implements the Scanner interface in the package fmt to scan ByteCount values from strings. This allows ByteCount values to be scanned from human-readable string representations with unit suffixes using the standard Scanf family functions in the package fmt; fmt.Scanf, fmt.Fscanf, and fmt.Sscanf.

For ByteCount type, four custom 'verbs' are implemented:

%s, %u	human-readable formats with both SI and binary prefixes
%S, %U	treat SI prefix as binary prefix; 1 kilobyte = 1024 bytes

Note that, unlike Format, the %s verb can properly scan expressions with units using both SI and binary prefixes.

Therefore, it is usually recommended to scan using only the %s verb. The %S verb is the same as %s, except that it treats the SI prefix as binary prefix. That is, %S scans the expression "100 kB" as 100 KiB (=102400 B).

For verbs %s and %S, unit suffix is mandatory. If the first token consists only of digits, it is assumed that the next token is a unit suffix, with one space in between. On the other hand, %u and %U do not allow expressions with a space between digits and the unit suffix. They always scan only one token. They assume that if the token consists only of digits, it is the number of bytes.

The following verbs are compatible with uint64 and scans integers without a unit suffix. If it is clear that there is absolutely no unit suffix in the input, the use of these is recommended:

%b	base 2
%o	base 8
%d	base 10
%x, %X	base 16

See the package fmt documentation for details.

Example (Sscanf)
src := []string{
	"kawaii.png is 1.23MB",
	"work.xlsx is 234.56 kibibytes",
	"huge.zip is 999 TB",
}
for _, s := range src {
	var file string
	var size infounit.ByteCount
	_, _ = fmt.Sscanf(s, "%s is %s", &file, &size)
	fmt.Println(file, size)
}
Output:

kawaii.png 1.2 MB
work.xlsx 240.2 kB
huge.zip 999.0 TB

func (ByteCount) String

func (bc ByteCount) String() string

String returns the human-readable string representing the byte count using SI prefix. This implements the Stringer interface in the package fmt.

func (*ByteCount) UnmarshalBinary

func (bc *ByteCount) UnmarshalBinary(data []byte) error

UnmarshalBinary decodes the ByteCount value from a binary form. This implements the BinaryUnmarshaler interface in the package encoding.

func (*ByteCount) UnmarshalJSON added in v1.1.2

func (bc *ByteCount) UnmarshalJSON(b []byte) error

UnmarshalJSON decodes the ByteCount value from a JSON field.

func (*ByteCount) UnmarshalText

func (bc *ByteCount) UnmarshalText(text []byte) error

UnmarshalText decodes the ByteCount value from a UTF-8-encoded text form. This implements the TextUnmarshaler interface in the package encoding.

func (*ByteCount) UnmarshalYAML added in v1.1.1

func (bc *ByteCount) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML decodes the ByteCount value from a YAML field.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL