convert

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KB = 1024
	MB = KB * 1024
	GB = MB * 1024
	TB = GB * 1024
	PB = TB * 1024
)

常用字节单位(IEC标准, 1024 进制)

Variables

This section is empty.

Functions

func BoolToInt

func BoolToInt(b bool) int

func Float64ToStr

func Float64ToStr(f float64, prec int) string

Float64ToStr (带精度控制)

func HumanBandwidth

func HumanBandwidth(bytesPerSec int64) string

HumanBandwidth 将字节/秒 转换为 Mbps, Gbps 等 网络流量速率转换 (适用于带宽、速率显示,如 Mbps、MB/s) 例如: 125000 -> "1.00 Mbps" (≈125 KB/s)

func HumanBytes

func HumanBytes(size int64) string

HumanBytes 将字节数格式化为可读字符串 例如: 1536 -> "1.50 KB"

func HumanDuration

func HumanDuration(d time.Duration) string

HumanDuration 将时间间隔格式化为可读字符串 例如: 90s -> "1m30s"

func Int64ToStr

func Int64ToStr(i int64) string

func IntToBool

func IntToBool(i int) bool

func IntToStr

func IntToStr(i int) string

func IntsToStrings

func IntsToStrings(arr []int) []string

IntsToStrings []int -> []string

func JSONToMapStrAny

func JSONToMapStrAny(s string) (map[string]any, error)

JSONToMapStrAny 转换 JSON -> map[string]any

func MapStrAnyToJSON

func MapStrAnyToJSON(m map[string]any) (string, error)

MapStrAnyToJSON 转换 map[string]any -> JSON 字符串

func MustStrToInt

func MustStrToInt(s string) int

MustStrToInt 忽略错误,失败返回 0

func ParseBytes

func ParseBytes(s string) (int64, error)

ParseBytes 将带单位的字符串转为字节数 支持: B, KB, MB, GB, TB, PB (不区分大小写) 例如: "1.5GB" -> 1610612736

func StrToBool

func StrToBool(s string) (bool, error)

StrToBool 支持 "true/false/1/0/yes/no"

func StrToFloat64

func StrToFloat64(s string) (float64, error)

func StrToInt

func StrToInt(s string) (int, error)

StrToInt 安全转换字符串到 int

func StrToInt64

func StrToInt64(s string) (int64, error)

func StrToUint64

func StrToUint64(s string) (uint64, error)

func StringsToInts

func StringsToInts(arr []string) ([]int, error)

StringsToInts []string -> []int

func ToInt64

func ToInt64(v interface{}) (int64, error)

ToInt64 任意类型转 int64

func ToString

func ToString(v interface{}) string

ToString 任意类型转 string

Types

This section is empty.

Jump to

Keyboard shortcuts

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