datasize

package
v1.6.4-pre.3 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: Apache-2.0 Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package datasize provides named units, parsing and formatting for sizes of data in bytes, and a flag value that accepts sizes such as "6MB".

Index

Constants

View Source
const (
	KB = 1 << 10
	MB = 1 << 20
	GB = 1 << 30
)

Units of data size. They are powers of 1024, so 6*MB is 6*1024*1024 bytes. They are untyped constants, so they can be used with any integer type.

Variables

This section is empty.

Functions

func Format

func Format(size int64) string

Format returns the size in the largest unit that represents it exactly, for example "6MB" for 6291456 and "1500B" for 1500. Parse reads the result back as the same size. A negative size is not a valid data size, and it is returned as a plain number.

func Parse

func Parse(s string) (int64, error)

Parse parses a data size such as "6MB", "512KB" or "6291456" and returns it in bytes. A number without a unit is a number of bytes. The units are B, KB, MB and GB, and KiB, MiB and GiB are accepted as other names for KB, MB and GB. Units are powers of 1024 and are not case sensitive. Fractions and negative sizes are not accepted.

Types

type Integer

type Integer interface {
	~int | ~int64
}

Integer is the constraint for the types of the variables that a Value can set.

type Value

type Value[T Integer] struct {
	// contains filtered or unexported fields
}

Value is a flag value for a data size. It implements the Value interfaces of both the flag and the pflag packages, and stores the size in bytes in an integer variable. It accepts everything that Parse accepts. It also accepts every number that the integer flags of the flag and pflag packages accept, with the same meaning, so a flag that used to take a number of bytes keeps working when it is changed to a Value.

func NewValue

func NewValue[T Integer](target *T, defaultSize T) *Value[T]

NewValue returns a Value that stores the size in target. It sets target to defaultSize, in the same way as flag.IntVar.

func (*Value[T]) Get

func (v *Value[T]) Get() any

Get returns the current size in the type of the target variable. It makes Value a flag.Getter, as the integer flags are.

func (*Value[T]) Set

func (v *Value[T]) Set(s string) error

Set parses s and stores the size in the target variable.

func (*Value[T]) String

func (v *Value[T]) String() string

String returns the current size in the format of Format.

func (*Value[T]) Type

func (v *Value[T]) Type() string

Type returns the name of the type of the flag for the pflag package.

Jump to

Keyboard shortcuts

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