size

package
v0.1.22 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package size 用于解析和格式化数据大小,并在常用的字节、位单位之间换算

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidFormat 表示输入不符合数据大小的书写格式
	ErrInvalidFormat = errors.New("invalid size format")
	// ErrInvalidUnit 表示单位无法识别
	ErrInvalidUnit = errors.New("invalid size unit")
	// ErrFractionalByte 表示换算结果包含不足一个字节的小数部分
	ErrFractionalByte = errors.New("size is not a whole number of bytes")
	// ErrOverflow 表示换算后的字节数超出 uint64 的取值范围
	ErrOverflow = errors.New("size overflows uint64")
	// ErrInvalidPrecision 表示小数位数小于 0
	ErrInvalidPrecision = errors.New("invalid size precision")
)
View Source
var ByteUnit = []string{"", "K", "M", "G", "T", "P"}

ByteUnit 是旧版 API 使用的单位前缀表

Deprecated: 改用 Unit 常量;解析单位字符串时使用 ParseUnit

Functions

This section is empty.

Types

type ByteSize

type ByteSize uint64

ByteSize 表示以字节为单位的数据大小

func B added in v0.1.21

func B(value uint64) ByteSize

func GB added in v0.1.21

func GB(value uint64) ByteSize

func GiB added in v0.1.21

func GiB(value uint64) ByteSize

func KB added in v0.1.21

func KB(value uint64) ByteSize

func KiB added in v0.1.21

func KiB(value uint64) ByteSize

func MB added in v0.1.21

func MB(value uint64) ByteSize

func MiB added in v0.1.21

func MiB(value uint64) ByteSize

func New added in v0.1.21

func New(value uint64, unit Unit) (ByteSize, error)

New 将指定单位的整数值换算为字节数。结果不是整数个字节、超出 uint64 范围或 unit 无效时返回相应错误

func PB added in v0.1.21

func PB(value uint64) ByteSize

func Parse added in v0.1.21

func Parse(input string) (ByteSize, error)

Parse 将形如 "1.5 MiB"、"8Kb" 的字符串换算为字节数 数值和单位之间可以留空格;不写单位时按字节处理。单位前缀及其中的 i 不区分大小写,但末尾的 B 和 b 分别表示字节和位,大小写含义不同

func ParseFromString deprecated

func ParseFromString(str string) (ByteSize, error)

ParseFromString 将字符串表示的数据大小换算为字节数

Deprecated: 改用 Parse

func PiB added in v0.1.21

func PiB(value uint64) ByteSize

func TB added in v0.1.21

func TB(value uint64) ByteSize

func TiB added in v0.1.21

func TiB(value uint64) ByteSize

func (ByteSize) Format added in v0.1.21

func (s ByteSize) Format(unit Unit, precision int) (string, error)

Format 按指定单位和小数位数格式化 s。结果采用四舍五入,数值和单位之间 不留空格,例如 ByteSize(1536).Format(Kibibyte, 2) 返回 "1.50KiB"

func (ByteSize) GB deprecated

func (s ByteSize) GB() ByteSizeUnit

Deprecated: 改用 s.In(Gigabyte)

func (ByteSize) GiB deprecated

func (s ByteSize) GiB() ByteSizeUnit

Deprecated: 改用 s.In(Gibibyte)

func (ByteSize) In added in v0.1.21

func (s ByteSize) In(unit Unit) (float64, error)

In 将 s 换算成指定单位。返回值为 float64,数值较大时可能损失精度

func (ByteSize) KB deprecated

func (s ByteSize) KB() ByteSizeUnit

Deprecated: 改用 s.In(Kilobyte)

func (ByteSize) KiB deprecated

func (s ByteSize) KiB() ByteSizeUnit

Deprecated: 改用 s.In(Kibibyte)

func (ByteSize) MB deprecated

func (s ByteSize) MB() ByteSizeUnit

Deprecated: 改用 s.In(Megabyte)

func (ByteSize) MiB deprecated

func (s ByteSize) MiB() ByteSizeUnit

Deprecated: 改用 s.In(Mebibyte)

func (ByteSize) PB deprecated

func (s ByteSize) PB() ByteSizeUnit

Deprecated: 改用 s.In(Petabyte)

func (ByteSize) PiB deprecated

func (s ByteSize) PiB() ByteSizeUnit

Deprecated: 改用 s.In(Pebibyte)

func (ByteSize) String added in v0.1.19

func (s ByteSize) String() string

String 自动选用不大于 s 的最大二进制字节单位,最多保留两位小数, 并省略小数末尾无意义的 0。例如 1536 字节会格式化为 "1.5KiB"

func (ByteSize) TB deprecated

func (s ByteSize) TB() ByteSizeUnit

Deprecated: 改用 s.In(Terabyte)

func (ByteSize) TiB deprecated

func (s ByteSize) TiB() ByteSizeUnit

Deprecated: 改用 s.In(Tebibyte)

type ByteSizeUnit deprecated

type ByteSizeUnit struct {
	Size float64
	Unit string
}

ByteSizeUnit 是旧版 API 中“数值 + 单位”的浮点表示

Deprecated: 单位换算改用 ByteSize.In,输出字符串改用 ByteSize.Format

func (ByteSizeUnit) String

func (b ByteSizeUnit) String() string

String 按旧版格式输出:固定保留两位小数,并在数值和单位之间留一个空格

func (ByteSizeUnit) ToByteSize deprecated added in v0.1.21

func (b ByteSizeUnit) ToByteSize() ByteSize

ToByteSize 将 b 换算为字节数,不足一个字节的部分直接舍去

Deprecated: 整数值改用 New;带小数的文本输入改用 Parse

type Unit added in v0.1.21

type Unit uint8

Unit 表示数据大小的计量单位,包括十进制、二进制的字节和位单位

const (
	Byte Unit = iota
	Kilobyte
	Megabyte
	Gigabyte
	Terabyte
	Petabyte
	Kibibyte
	Mebibyte
	Gibibyte
	Tebibyte
	Pebibyte
	Bit
	Kilobit
	Megabit
	Gigabit
	Terabit
	Petabit
	Kibibit
	Mebibit
	Gibibit
	Tebibit
	Pebibit
)

func ParseUnit added in v0.1.21

func ParseUnit(input string) (Unit, error)

ParseUnit 解析单位字符串。K、M、G、T、P 以及二进制单位中的 i 均不区分 大小写;末尾的大写 B 表示字节,小写 b 表示位

func (Unit) String added in v0.1.21

func (u Unit) String() string

String 返回单位的规范写法,例如 Kibibyte 返回 "KiB",Kilobit 返回 "Kb"

Jump to

Keyboard shortcuts

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