size

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2026 License: MIT Imports: 1 Imported by: 6

Documentation

Index

Constants

View Source
const (
	Bytes    Size = 1
	Kibibyte      = Bytes * iecUnitFactor
	Mebibyte      = Kibibyte * iecUnitFactor
	Gibibyte      = Mebibyte * iecUnitFactor
	Tebibyte      = Gibibyte * iecUnitFactor
	Pebibyte      = Tebibyte * iecUnitFactor
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Sizable

type Sizable interface {
	// SizeOf returns the size of this entity.
	SizeOf() Size
}

Sizable is an interface describing a type that may have its runtime memory resident size estimated.

type Size

type Size uintptr

func Of

func Of[T any](templatedValue T) Size

Of returns the size of an object. When the input is a pointer to an object, the size of the pointer itself is returned, as opposed to the size of the memory referenced. This function cannot infer the data type of nil, so nil checks need to be performed before calling this function.

func OfAny

func OfAny(value any) Size

OfAny attempts to return the size of a value passed as `any`. This is done via type negotiation to unwrap the `any` type in a best-effort attempt to estimate the value's size.

It is important to note that this function comes with usage caveats: * Maps are not estimated by this function * Nested slices of type `[]any` are supported but have ill-defined runtime measurement costs * If the type of the value is known, this function is less performant that calling Of(...)

func OfAnySlice

func OfAnySlice(anySlice []any) Size

OfAnySlice calculates the estimated size of a given slice of type `[]any`.

func OfSlice

func OfSlice[T comparable](sliceT []T) Size

OfSlice will return the size of a type templated slice.

The slice descriptor consists of three fields: * A pointer to the underlying array. * The length of the slice (the number of elements currently in use). * The capacity of the slice (the total number of elements in the underlying array, from the start of the slice).

On a 64-bit architecture, each of these fields is 8 bytes, so the size of a slice is 24 bytes. On a 32-bit architecture, each field is 4 bytes, making the slice size 12 bytes. The unsafe.Sizeof() function will return this size of the descriptor, not the size of the underlying array.

For correctly typed templates the capacity of the slice is multiplied by the size of the template.

func (Size) Bytes

func (s Size) Bytes() uintptr

func (Size) Gibibyte

func (s Size) Gibibyte() float64

func (Size) Kibibytes

func (s Size) Kibibytes() float64

func (Size) Mebibytes

func (s Size) Mebibytes() float64

func (Size) Pebibyte

func (s Size) Pebibyte() float64

func (Size) Tebibyte

func (s Size) Tebibyte() float64

Jump to

Keyboard shortcuts

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