units

package
v0.197.0 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

README

Package units

The units package in Go provides a set of constants for commonly used units, like the size units for digital data.

These constants can be used to represent byte sizes in a more readable and consistent manner, similar to how the time package provides constants for durations like seconds and minutes.

bs := make([]byte, units.Megabyte)
r := io.LimitReader(buf, 128*units.Kibibyte)

For debugging purposes, you can even format a value which represent byte size with FormatByteSize.

fmt.Println(units.FormatByteSize(n))
// 1.51KiB

Documentation

Overview

Example (ByteSize)
package main

import (
	"bytes"
	"go.llib.dev/frameless/pkg/units"
	"io"
)

func main() {

	var bs = make([]byte, units.Megabyte)
	buf := &bytes.Buffer{}

	n, err := buf.Write(bs)
	if err != nil {
		panic(err.Error())
	}

	if n < units.Kilobyte {
		//
	}

	io.LimitReader(buf, 128*units.Kibibyte)
}

Index

Examples

Constants

View Source
const (
	Byte = 1 << (10 * iota) // ignore first value by assigning to blank identifier
	Kibibyte
	Mebibyte
	Gibibyte
	Tebibyte
	Pebibyte
)
View Source
const (
	Kilobyte = Kibibyte
	Megabyte = Mebibyte
	Gigabyte = Gibibyte
	Terabyte = Tebibyte
)

Variables

This section is empty.

Functions

func FormatByteSize

func FormatByteSize[ByteSize byteSize](n ByteSize) string

FormatByteSize will format byte size interpreted as the unit of digital information. Historically, the byte was the number of bits used to encode a single character of text in a computer and for this reason it is the smallest addressable unit of memory in many computer architectures

| Value | IEC | Memory | |-------|--------------|--------------| | 1 | B byte | B byte | | 1024 | KiB kibibyte | KB kilobyte | | 10242 | MiB mebibyte | MB megabyte | | 10243 | GiB gibibyte | GB gigabyte | | 10244 | TiB tebibyte | TB terabyte | | 10245 | PiB pebibyte | – | | 10246 | EiB exbibyte | – | | 10247 | ZiB zebibyte | – | | 10248 | YiB yobibyte | – |

Types

type ByteSize added in v0.188.0

type ByteSize = int64

Jump to

Keyboard shortcuts

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