bytesize

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2023 License: BSD-3-Clause Imports: 4 Imported by: 1

README

bytesize

Description

Provides a function to print quantities of bytes in human-readable formats, and to parse human-readable amounts of bytes into int64 (where possible) or float64.

When formatting, allows you to choose precision (number of decimals) and whether you would prefer IEC or SI units (binary or base 10).

Modeled on strconv and the FormatInt / FormatFloat functions:

bytesize.FormatBytes(value int, base int, precision int) string
bytesize.ParseBytes(value string) (int64, error)
bytesize.ParseBytesFloat(value string) (float64, error)

Examples:

bytesize.FormatBytes(1024, 2, 1) = "1.0KiB"
bytesize.FormatBytes(2000000, 10, 2) = "2.00MB"
bytesize.ParseBytes("6 GiB") = 6442450944

License

BSD-style, because this particular wheel really doesn't need reinventing.

Rationale

While "Effective Go" has sample code for a ByteSize type, it has a number of problems:

  1. It doesn't support variable precision, and always returns two decimals.
  2. It gets the units wrong, using SI prefixes for binary units.
  3. It doesn't let you choose between SI and IEC units.
  4. It requires that you cast your quantities to ByteSize type.

Also, it doesn't handle parsing.

Additional information

Documentation

Overview

Package bytesize provides a function for formatting and parsing quantities of data as human-readable values such as 1MB or 2.2GB.

Index

Constants

View Source
const (
	KB float64 = 1000
	MB         = 1000 * KB
	GB         = 1000 * MB
	TB         = 1000 * GB
	PB         = 1000 * TB
	EB         = 1000 * PB
	ZB         = 1000 * EB
	YB         = 1000 * ZB
)

SI (base 10) units See http://physics.nist.gov/cuu/Units/prefixes.html

View Source
const (
	KiB float64 = 1 << (10 * iota)
	MiB
	GiB
	TiB
	PiB
	EiB
	ZiB
	YiB
)

IEC (base 2) units See http://physics.nist.gov/cuu/Units/binary.html and https://en.wikipedia.org/wiki/Binary_prefix

Variables

This section is empty.

Functions

func FormatBytes

func FormatBytes(bytes int64, base int, prec int) string

FormatBytes formats the specified number of bytes in human-readable format, in either base 2 (IEC) or base 10 (SI) units as specified by the base parameter, with the specified number of digits of precision.

e.g.

FormatBytes(1024*1024, 2, 2) => "1.00MiB"
FormatBytes(2000000000, 10, 2) => "2.00GB"

If an invalid base or precision is given, a Sprintf-style error string is returned, starting %! followed by an error code in parentheses.

func ParseBytes

func ParseBytes(s string) (int64, error)

ParseBytes parses a human-readable quantity of bytes, and returns the raw number of bytes as an int64. If the value is too large for an int64, an error value is returned.

Whitespace is allowed between the number and the units. The 'B' for bytes is required to be upper case; 'b' in lower case would be bits.

func ParseBytesFloat

func ParseBytesFloat(s string) (float64, error)

ParseBytesFloat parses a human-readable quantity of bytes, and returns the raw number of bytes as a float64.

Whitespace is allowed between the number and the units. The 'B' for bytes is required to be upper case; 'b' in lower case would be bits.

Types

This section is empty.

Jump to

Keyboard shortcuts

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