sizes

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2023 License: MIT Imports: 0 Imported by: 5

README

Unit tests pkg.go.dev

Sizes

sizes is a Golang package that attempts to produce byte sizes that are obvious for the reader, instead of having nondescript calculations and depending on developers to comment what they were calculating.

Caveats

Overflow risks: If you're computing the byte size of many exabytes, you might run into overflow issues, since the calculations return uint64 and might run out of space for the real number.

Wrong use case: If you're doing many calculations where you compute some byte sizes, it might be faster for you to do the calculations by hand, as a builder pattern is used here and will create a struct each time you calculate a size.

Examples

Calculating 100 megabytes in bytes:

sizes.Megabytes(100)

Bytes are the default unit that the sizes are returned in. If you wish to calculate 100 megabytes in kilobytes:

sizes.MegabytesAs(100, sizes.Kilobyte)

You can also go the other way, calculating from lower units to larger.

Calculating 2 million bytes as kilobytes (please be aware of rounding down since dealing with integers):

sizes.BytesAs(2_000_000, sizes.Megabyte)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bytes

func Bytes[T AnyInteger](amount uint64) T

Bytes returns the size in bytes.

func BytesAs added in v0.2.0

func BytesAs[T AnyInteger](amount uint64, unit Size) T

BytesAs returns the size in bytes as the specified type.

func Exabytes

func Exabytes[T AnyInteger](amount uint64) T

Exabytes returns the size in exabytes.

func ExabytesAs added in v0.2.0

func ExabytesAs[T AnyInteger](amount uint64, unit Size) T

ExabytesAs returns the size in exabytes as the specified type.

func Gigabytes

func Gigabytes[T AnyInteger](amount uint64) T

Gigabytes returns the size in gigabytes.

func GigabytesAs added in v0.2.0

func GigabytesAs[T AnyInteger](amount uint64, unit Size) T

GigabytesAs returns the size in gigabytes as the specified type.

func Kilobytes

func Kilobytes[T AnyInteger](amount uint64) T

Kilobytes returns the size in kilobytes.

func KilobytesAs added in v0.2.0

func KilobytesAs[T AnyInteger](amount uint64, unit Size) T

KilobytesAs returns the size in kilobytes as the specified type.

func Megabytes

func Megabytes[T AnyInteger](amount uint64) T

Megabytes returns the size in megabytes.

func MegabytesAs added in v0.2.0

func MegabytesAs[T AnyInteger](amount uint64, unit Size) T

MegabytesAs returns the size in megabytes as the specified type.

func Petabytes

func Petabytes[T AnyInteger](amount uint64) T

Petabytes returns the size in petabytes.

func PetabytesAs added in v0.2.0

func PetabytesAs[T AnyInteger](amount uint64, unit Size) T

PetabytesAs returns the size in petabytes as the specified type.

func Terabytes

func Terabytes[T AnyInteger](amount uint64) T

Terabytes returns the size in terabytes.

func TerabytesAs added in v0.2.0

func TerabytesAs[T AnyInteger](amount uint64, unit Size) T

TerabytesAs returns the size in terabytes as the specified type.

Types

type AnyInteger added in v0.3.0

type AnyInteger interface {
	int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64
}

type Size

type Size uint64

Size is a type that represents a size in bytes.

const (
	Byte Size = 1 << (10 * iota)
	Kilobyte
	Megabyte
	Gigabyte
	Terabyte
	Petabyte
	Exabyte
)

func (Size) Uint64

func (s Size) Uint64() uint64

Uint64 returns the Size as an uint64.

Jump to

Keyboard shortcuts

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