Documentation
¶
Overview ¶
Package size 用于解析和格式化数据大小,并在常用的字节、位单位之间换算
Index ¶
- Variables
- type ByteSize
- func B(value uint64) ByteSize
- func GB(value uint64) ByteSize
- func GiB(value uint64) ByteSize
- func KB(value uint64) ByteSize
- func KiB(value uint64) ByteSize
- func MB(value uint64) ByteSize
- func MiB(value uint64) ByteSize
- func New(value uint64, unit Unit) (ByteSize, error)
- func PB(value uint64) ByteSize
- func Parse(input string) (ByteSize, error)
- func ParseFromString(str string) (ByteSize, error)deprecated
- func PiB(value uint64) ByteSize
- func TB(value uint64) ByteSize
- func TiB(value uint64) ByteSize
- func (s ByteSize) Format(unit Unit, precision int) (string, error)
- func (s ByteSize) GB() ByteSizeUnitdeprecated
- func (s ByteSize) GiB() ByteSizeUnitdeprecated
- func (s ByteSize) In(unit Unit) (float64, error)
- func (s ByteSize) KB() ByteSizeUnitdeprecated
- func (s ByteSize) KiB() ByteSizeUnitdeprecated
- func (s ByteSize) MB() ByteSizeUnitdeprecated
- func (s ByteSize) MiB() ByteSizeUnitdeprecated
- func (s ByteSize) PB() ByteSizeUnitdeprecated
- func (s ByteSize) PiB() ByteSizeUnitdeprecated
- func (s ByteSize) String() string
- func (s ByteSize) TB() ByteSizeUnitdeprecated
- func (s ByteSize) TiB() ByteSizeUnitdeprecated
- type ByteSizeUnitdeprecated
- type Unit
Constants ¶
This section is empty.
Variables ¶
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") )
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 Parse ¶ added in v0.1.21
Parse 将形如 "1.5 MiB"、"8Kb" 的字符串换算为字节数 数值和单位之间可以留空格;不写单位时按字节处理。单位前缀及其中的 i 不区分大小写,但末尾的 B 和 b 分别表示字节和位,大小写含义不同
func ParseFromString
deprecated
func (ByteSize) Format ¶ added in v0.1.21
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) 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
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
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