sizefmt

package module
v0.0.0-...-26ec7d6 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2025 License: MIT Imports: 2 Imported by: 0

README

Go Byte Size formatter

This module exposes an int64 alias as Size which can be used for improved size string output with the fmt API.

It supports the following format "verbs":

  • %I - SI decimal units: kB(1000), MB(10002), GB(10003), TB(10004), PB(10005), EB(10006)
  • %B - binary ISO/IEC units: KiB kibibyte(1024), MiB mebibyte(10242) GiB gibibyte(10243) TiB tebibyte(10244) PiB pebibyte(10245) EiB exbibyte(10246)
  • %J - Binary JDEC units: KB, MB, GB, TB...

They correspond to a hybrid verb corresponding to %d if the value is smaller than 1000 (for SI) or 1024 (for ISO/JDEC) verbs, and that of %f - supporting the same width, precision, signedness and additional flags.

Additionally, we support %s which corresponds to %.2I.

See below for an explanation.

  • '+' always print a sign for numeric values
  • '-' pad with spaces on the right rather than the left (left-justify the field)
  • '#' alternate format: always print a decimal point; if used with %s it uses the corresponding ISO formatting %.2B
  • ' ' (space) leave a space for elided sign in numbers (% d); put spaces between bytes printing strings or slices in hex (% x, % X)
  • '0' pad with leading zeros rather than spaces; this moves the padding after the sign

We don't support suffixes past Exa due to the fact that values start to overflow the underlying int64 type.

Example:

package main

import (
	"fmt"
	"math"

	bytes "git.sr.ht/~mariusor/sizefmt"
)

func main() {
    const notSoManyBytes = bytes.Size(4096)
    const manyBytes = bytes.Size(math.MaxInt64)

    fmt.Printf("not so many bytes: %.2I %.2B %.2J\n", notSoManyBytes, notSoManyBytes, notSoManyBytes)
    fmt.Printf("so many bytes: %.3I %.3B %.3J", manyBytes, manyBytes, manyBytes)

    // Output: not so many bytes: 4.10kB 4.00KiB 4.00KB
    // so many bytes: 9.223EB 8.000EiB 8.000EB
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Size

type Size int64

Size is an integer alias that allows for custom string formatting options * SI Decimal units: kB(1000), MB(1000^2), GB(1000^3), TB(1000^4), PB(1000^5), EB(1000^6) * Binary ISO/IEC units: KiB kibibyte(1024), MiB mebibyte(1024^2) GiB gibibyte(1024^3) TiB tebibyte(1024^4) PiB pebibyte(1024^5) EiB exbibyte(1024^6) * Binary JDEC units[3]: KB, MB, GB, TB Past "Exa" we're outside what int64 can represent, so we stop there.

[1] https://en.wikipedia.org/wiki/Byte#Multiple-byte_units

func (Size) Format

func (s Size) Format(st fmt.State, r rune)

Format outputs the three types of representation.

Example
const notSoManyBytes = Size(4096)
const manyBytes = Size(math.MaxInt64)

fmt.Printf("not so many bytes: %.2I %.2B %.2J\n", notSoManyBytes, notSoManyBytes, notSoManyBytes)
fmt.Printf("so many bytes: %.3I %.3B %.3J", manyBytes, manyBytes, manyBytes)
Output:

not so many bytes: 4.10kB 4.00KiB 4.00KB
so many bytes: 9.223EB 8.000EiB 8.000EB

Jump to

Keyboard shortcuts

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