bytecount

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2025 License: MIT Imports: 2 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. The highest byte count printable is 268435440QB (Quetta-bytes, 10³⁰), exceeding this will print +InfQB.

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).

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

Types

type Value

type Value float32

func N

func N[T numeric](n T) Value
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)

func (Value) String added in v1.3.0

func (v Value) String() string

Jump to

Keyboard shortcuts

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