unit

package
v0.1.83 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ByteSizeFormatFloat = DefaultByteSizeFormatFloat

ByteSizeFormatFloat defines how floating-point values are formatted in String(). It can be replaced to customize output precision.

Functions

func DefaultByteSizeFormatFloat

func DefaultByteSizeFormatFloat(f float64) string

DefaultByteSizeFormatFloat formats a float with up to 2 decimals, trimming trailing zeros and the decimal point if unnecessary.

Types

type ByteSize

type ByteSize int64

ByteSize represents a quantity of bytes.

const (
	B  ByteSize = 1
	KB ByteSize = 1 << (10 * iota)
	MB
	GB
	TB
	PB
	EB
)

Common byte size units.

func MustParseByteSize added in v0.1.81

func MustParseByteSize(s string) ByteSize

MustParseByteSize parses a size string and panics if it is invalid.

func NewByteSize

func NewByteSize(n float64, unit string) (ByteSize, error)

NewByteSize creates a ByteSize from a numeric value and a unit string. Example: NewByteSize(1.5, "GB") -> 1.5GB. Returns an error if the unit is not recognized.

func ParseByteSize

func ParseByteSize(s string) (ByteSize, error)

ParseByteSize parses a human-readable size string (e.g. "1.5GB", "100 KB") and returns the corresponding ByteSize value.

func (ByteSize) MarshalText

func (b ByteSize) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (ByteSize) String

func (n ByteSize) String() string

String returns a human-readable representation of the byte size. e.g. 1536 -> "1.5KB", 1048576 -> "1MB".

func (ByteSize) To added in v0.1.81

func (b ByteSize) To(unit string, decimals int) (string, error)

To converts the ByteSize to the specified unit and returns its string representation.

func (*ByteSize) UnmarshalText

func (b *ByteSize) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

Jump to

Keyboard shortcuts

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