bytecount

package module
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: May 26, 2026 License: MIT Imports: 3 Imported by: 2

README

build coverage goreport Docs

bytecount

Go pretty printer for byte counts.

Only depends on the standard library.

Usage

go get github.com/linkdata/bytecount

When printing byte counts using the fmt package, pass the values using bytecount.N(n).

You may pass width and precision if you wish. The default is to keep the output to a maximum of six characters while still showing as much precision as possible.

bytecount.Value stores values as float32, so the largest representable byte count is math.MaxFloat32 (~3.4×10³⁸), which formats as 268435440QB (Quetta-bytes use the SI prefix Q = 10³⁰). Anything larger overflows the underlying float32 and prints as +InfQB. Exact integer precision is also limited to 24 bits (16,777,216); above that, neighboring integer byte counts may format identically.

If the formatting verb is d (e.g. "%d"), the divisor is 1000 rather than 1024.

If the formatting verb is b (e.g. "%b"), the value is multiplied by 8 and the unit suffix is changed from B (bytes) to b (bits). The divisor stays at 1024; combining bit output with an SI divisor is not supported.

If the "#" flag is given, no unit suffix is written.

If the " " flag is given, a space is written between the digits and the suffix.

Example

import (
	"fmt"

	"github.com/linkdata/bytecount"
)

func main() {
	fmt.Print(bytecount.N(53667551))
	// Output:
	// 51.2MB
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Sprint

func Sprint[T numeric](v T) string

Sprint returns the default human-readable formatting of v, equivalent to fmt.Sprint(N(v)).

Types

type Value

type Value float32

Value is a byte count expressed as a float32, formatted via Value.Format as a short human-readable string with an SI-style scale prefix and a unit suffix (`B` for bytes, `b` for bits). The zero value formats as "0B".

func N

func N[T numeric](n T) Value

N converts any numeric value to a Value suitable for passing to the fmt package. Conversion goes through float32, so very large or very precise integers may lose precision.

Example
package main

import (
	"fmt"
	"math"

	"github.com/linkdata/bytecount"
)

func main() {
	var n int64
	n = 64
	for i := 0; i < 10; i++ {
		n++
		n *= 7
		fmt.Printf("%v %v\n", n, bytecount.N(n))
	}
	fmt.Printf("%v %v\n", math.MaxFloat32, bytecount.N(math.MaxFloat32))
}
Output:
455 455B
3192 3.12kB
22351 21.8kB
156464 153kB
1095255 1.04MB
7666792 7.31MB
53667551 51.2MB
375672864 358MB
2629710055 2.45GB
18407970392 17.1GB
3.4028234663852886e+38 268435440QB

func (Value) Format

func (v Value) Format(f fmt.State, verb rune)

Format implements fmt.Formatter. It supports the verbs `v`, `d`, and `b`:

  • `v`: bytes with a 1024 divisor and `B` suffix.
  • `d`: bytes with a 1000 (SI) divisor and `B` suffix.
  • `b`: bits (value multiplied by 8) with a 1024 divisor and `b` suffix.

The `#` flag omits the unit suffix. The ` ` (space) flag inserts a space between the digits and the scale/unit. The `+`, `-`, `0`, width, and precision flags behave as in the standard fmt package. Unsupported verbs produce the usual `%!verb(type=value)` error string.

func (Value) String added in v1.3.0

func (v Value) String() string

String returns the default formatting of v, equivalent to fmt.Sprint(v).

Jump to

Keyboard shortcuts

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