Documentation
¶
Index ¶
- Constants
- Variables
- func B2S(b []byte) string
- func B64Decode(s string) []byte
- func B64Encode(b []byte) string
- func B64UrlDecode(s string) []byte
- func B64UrlEncode(b []byte) string
- func BeginOfDay(t time.Time) time.Time
- func BeginOfHour(t time.Time) time.Time
- func BeginOfLastMonth(t time.Time) time.Time
- func BeginOfLastWeek(t time.Time) time.Time
- func BeginOfMinute(t time.Time) time.Time
- func BeginOfMonth(t time.Time) time.Time
- func BeginOfNextMonth(t time.Time) time.Time
- func BeginOfNextWeek(t time.Time) time.Time
- func BeginOfSecond(t time.Time) time.Time
- func BeginOfTomorrow(t time.Time) time.Time
- func BeginOfWeek(t time.Time) time.Time
- func BeginOfYear(t time.Time) time.Time
- func BeginOfYesterday(t time.Time) time.Time
- func BigComma(b *big.Int) string
- func BigCommaf(v *big.Float) string
- func Bigoom(n, b *big.Int) (float64, int)
- func CPUTicks() int64
- func CallPath() string
- func CheckDomain(name string) error
- func Comma(v int64) string
- func Commaf(v float64) string
- func Commai(v int) string
- func Commau(v uint64) string
- func CopyB2S(b []byte) string
- func CopyBytes(b []byte) []byte
- func CopyS2B(s string) []byte
- func CopyString(s string) string
- func CutBytes(s, sep []byte) (before, after []byte, found bool)
- func CutString(s, sep string) (before, after string, found bool)
- func DaysInMonth(year int, m time.Month) int
- func DaysInYear(year int) int
- func EncodeUUID(id []byte) []byte
- func EndOfDay(t time.Time) time.Time
- func EndOfHour(t time.Time) time.Time
- func EndOfLastMonth(t time.Time) time.Time
- func EndOfLastWeek(t time.Time) time.Time
- func EndOfMinute(t time.Time) time.Time
- func EndOfMonth(t time.Time) time.Time
- func EndOfNextMonth(t time.Time) time.Time
- func EndOfNextWeek(t time.Time) time.Time
- func EndOfSecond(t time.Time) time.Time
- func EndOfTomorrow(t time.Time) time.Time
- func EndOfWeek(t time.Time) time.Time
- func EndOfYear(t time.Time) time.Time
- func EndOfYesterday(t time.Time) time.Time
- func EqualFold(b, s string) bool
- func EqualFoldBytes(b, s []byte) bool
- func Executable(evalSymlinks ...bool) string
- func ExecutableDir(evalSymlinks ...bool) string
- func FastIntn(n int) int
- func FastRand() uint32
- func FastRand64() uint64
- func FastRandn(n uint32) uint32
- func FastRandu() uint
- func FirstString(s, sep string, defaultStr ...string) string
- func GetBytes(v any, defaultVal ...[]byte) []byte
- func GetDomain(name string) string
- func GetIPPort(addr net.Addr) (ip net.IP, port int, err error)
- func GetInt(v any, defaultInt ...int) int
- func GetMonthDays(t time.Time) int
- func GetNotInternalIPv4(ip, defaultIP net.IP, flag ...bool) net.IP
- func GetNotInternalIPv4String(ip, defaultIP string, flag ...bool) string
- func GetSafeB2S(b []byte, defaultVal ...string) string
- func GetSafeBytes(b []byte, defaultVal ...[]byte) []byte
- func GetSafeS2B(s string, defaultVal ...[]byte) []byte
- func GetSafeString(s string, defaultVal ...string) string
- func GetString(v any, defaultVal ...string) string
- func GoroutineID() (uint64, error)
- func Gzip(data []byte) ([]byte, error)
- func GzipLevel(data []byte, level int) (dst []byte, err error)
- func HumanBaseBytes(v uint64, base float64, sizes []string) string
- func HumanBigBytes(s *big.Int) string
- func HumanBigIBytes(s *big.Int) string
- func HumanBigKbps(s *big.Int) string
- func HumanBytes(v uint64) string
- func HumanGBMB(v uint64) string
- func HumanIBytes(v uint64) string
- func HumanIntBytes(v int) string
- func HumanIntIBytes(v int) string
- func HumanIntKbps(v int) string
- func HumanKbps(v uint64) string
- func ID() uint64
- func IPv4String2Long(ip string) int
- func IPv4String2LongLittle(ip string) int
- func IPv6String2Int(ip string) *big.Int
- func IPv42Long(ip net.IP) int
- func IPv42LongLittle(ip net.IP) int
- func IPv62Int(ip net.IP) *big.Int
- func InIPNet(ip net.IP, ipNets map[*net.IPNet]struct{}) bool
- func InIPNetString(ip string, ipNets map[*net.IPNet]struct{}) bool
- func InInts(slice []int, n int) bool
- func InStrings(ss []string, s string) bool
- func InitCSTLocation() (name string, loc *time.Location, cst *time.Location, ok bool)
- func InitChinaLocation() *time.Location
- func InitLocation(name string) (*time.Location, bool)
- func Int2IPv6(ipInt *big.Int) net.IP
- func Int2IPv6String(n *big.Int) string
- func IsIP(ip string) bool
- func IsIPv4(s string) bool
- func IsIPv6(s string) bool
- func IsInternalIPv4(ip net.IP) bool
- func IsInternalIPv4String(ip string) bool
- func IsLeapYear(year int) bool
- func IsLetter(s string) bool
- func IsLetterOrNumeric(s string) bool
- func IsNumeric(s string) bool
- func IsPrivateIP(ip net.IP) bool
- func IsPrivateIPString(ip string) bool
- func JoinBytes(b ...[]byte) []byte
- func JoinString(s ...string) string
- func JoinStringBytes(s ...string) []byte
- func LastString(s, sep string, defaultStr ...string) string
- func LeftPad(s, pad string, n int) string
- func LeftPadBytes(b, pad []byte, n int) []byte
- func Logn(n, b float64) float64
- func Long2IPv4(n int) net.IP
- func Long2IPv4String(n int) string
- func LongLittle2IPv4(n int) net.IP
- func LongLittle2IPv4String(n int) string
- func MaxInt(a, b int) int
- func MinInt(a, b int) int
- func MustBool(v any) bool
- func MustInt(v any) int
- func MustJSON(v any) []byte
- func MustJSONIndent(v any) []byte
- func MustJSONIndentString(v any) string
- func MustJSONString(v any) string
- func MustParseHumanBigBytes(s string, defaultVal ...*big.Int) *big.Int
- func MustParseHumanBytes(s string, defaultVal ...uint64) uint64
- func MustString(v any, timeLayout ...string) string
- func NanoTime() int64
- func NewRand(seed ...int64) *rand.Rand
- func NewSpinLock() sync.Locker
- func Pad(s, pad string, n int) string
- func PadBytes(s, pad []byte, n int) []byte
- func ParseHostPort(s string) (net.IP, uint16, bool, error)
- func ParseHumanBigBytes(s string) (*big.Int, error)
- func ParseHumanBytes(s string) (uint64, error)
- func ParseIP(s string) (net.IP, bool)
- func ParseIPv4(ip string) net.IP
- func ParseIPv6(ip string) net.IP
- func ParseIPx(s string) (net.IP, bool)
- func ParseIPxWithNumeric(s string) (net.IP, bool)
- func ParseInts(s string) ([]int, error)
- func RandAlphaBytes(n int) []byte
- func RandAlphaString(n int) string
- func RandBytes(n int) []byte
- func RandBytesLetters(n int, letters string) []byte
- func RandDecBytes(n int) []byte
- func RandDecString(n int) string
- func RandHexBytes(n int) []byte
- func RandHexString(n int) string
- func RandInt(min, max int) int
- func RandString(n int) string
- func RandUint32(min, max uint32) uint32
- func Recover(cb ...RecoveryCallback)
- func RemoveRunes(s string, toRemove string) string
- func RemoveString(ss []string, s string) ([]string, bool)
- func ReplaceHost(a, b string) string
- func Reverse(s string) string
- func RightPad(s, pad string, n int) string
- func RightPadBytes(b, pad []byte, n int) []byte
- func Round(v float64, precision int) float64
- func RunPath() string
- func RuneReverse(s string) string
- func RuneSubString(s string, length int, suffix string) string
- func S2B(s string) []byte
- func SafeGo(fn func(), cb ...RecoveryCallback)
- func SafeGoCommonFunc(args any, fn func(args any), cb ...RecoveryCallback)
- func SafeGoWithContext(ctx context.Context, fn func(ctx context.Context), cb ...RecoveryCallback)
- func SearchInt(slice []int, n int) int
- func SearchString(ss []string, s string) int
- func Sleep(ctx context.Context, interval time.Duration) error
- func SplitHostPort(hostPort string) (host, port string)
- func SumInt(v ...int) int
- func ToLower(b string) string
- func ToLowerBytes(b []byte) []byte
- func ToUpper(b string) string
- func ToUpperBytes(b []byte) []byte
- func Trim(s string, cutset byte) string
- func TrimBOMSpace(s string) string
- func TrimBytes(b []byte, cutset byte) []byte
- func TrimLeft(s string, cutset byte) string
- func TrimLeftBytes(b []byte, cutset byte) []byte
- func TrimRight(s string, cutset byte) string
- func TrimRightBytes(b []byte, cutset byte) []byte
- func TrimSlice(ss []string) []string
- func TruncStr(s string, maxLen int, suffix string) string
- func TruncStrTail(s string, maxLen int, suffix string) string
- func UUID() []byte
- func UUIDShort() string
- func UUIDSimple() string
- func UUIDString() string
- func Ungzip(data []byte) (src []byte, err error)
- func Unzip(data []byte) (src []byte, err error)
- func ValidOptionalPort(port string) bool
- func WaitNextMinute(t ...time.Time)
- func WaitNextMinuteWithTime(t ...time.Time) (now time.Time)
- func WaitNextSecond(t ...time.Time)
- func WaitNextSecondWithTime(t ...time.Time) (now time.Time)
- func WaitSignal(sig ...os.Signal) os.Signal
- func WaitUntilMinute(m int, t ...time.Time)
- func WaitUntilSecond(s int, t ...time.Time)
- func Zip(data []byte) ([]byte, error)
- func ZipLevel(data []byte, level int) (dst []byte, err error)
- type Bool
- func (x *Bool) CAS(old, new bool) bool
- func (x *Bool) CompareAndSwap(old, new bool) (swapped bool)
- func (x *Bool) Load() bool
- func (x *Bool) MarshalJSON() ([]byte, error)
- func (x *Bool) Store(val bool)
- func (x *Bool) StoreFalse()
- func (x *Bool) StoreTrue()
- func (x *Bool) String() string
- func (x *Bool) Swap(new bool) (old bool)
- func (x *Bool) Toggle() (old bool)
- func (x *Bool) UnmarshalJSON(b []byte) error
- type NoCmp
- type NoCopy
- type RecoveryCallback
- type TryMutex
Constants ¶
const ( Byte = 1 << (iota * 10) KiByte MiByte GiByte TiByte PiByte EiByte )
Ref: dustin/go-humanize IEC Sizes. kibis of bits
const ( IByte = 1 KByte = IByte * 1000 MByte = KByte * 1000 GByte = MByte * 1000 TByte = GByte * 1000 PByte = TByte * 1000 EByte = PByte * 1000 )
SI Sizes.
const ( IPv4Min = 0 IPv4Max = 1<<32 - 1 )
const ( ChinaTimeZone = "UTC+8" ChinaTimeOffset = 8 * 60 * 60 )
const ( // PtrSize 4 on 32-bit systems, 8 on 64-bit. PtrSize = 4 << (^uintptr(0) >> 63) )
Variables ¶
var ( // BigByte is one byte in bit.Ints BigByte = big.NewInt(1) // BigKiByte is 1,024 bytes in bit.Ints BigKiByte = (&big.Int{}).Mul(BigByte, bigIECExp) // BigMiByte is 1,024 k bytes in bit.Ints BigMiByte = (&big.Int{}).Mul(BigKiByte, bigIECExp) // BigGiByte is 1,024 m bytes in bit.Ints BigGiByte = (&big.Int{}).Mul(BigMiByte, bigIECExp) // BigTiByte is 1,024 g bytes in bit.Ints BigTiByte = (&big.Int{}).Mul(BigGiByte, bigIECExp) // BigPiByte is 1,024 t bytes in bit.Ints BigPiByte = (&big.Int{}).Mul(BigTiByte, bigIECExp) // BigEiByte is 1,024 p bytes in bit.Ints BigEiByte = (&big.Int{}).Mul(BigPiByte, bigIECExp) // BigZiByte is 1,024 e bytes in bit.Ints BigZiByte = (&big.Int{}).Mul(BigEiByte, bigIECExp) // BigYiByte is 1,024 z bytes in bit.Ints BigYiByte = (&big.Int{}).Mul(BigZiByte, bigIECExp) )
Ref: dustin/go-humanize
var ( // BigSIByte is one SI byte in big.Ints BigSIByte = big.NewInt(1) // BigKByte is 1,000 SI bytes in big.Ints BigKByte = (&big.Int{}).Mul(BigSIByte, bigSIExp) // BigMByte is 1,000 SI k bytes in big.Ints BigMByte = (&big.Int{}).Mul(BigKByte, bigSIExp) // BigGByte is 1,000 SI m bytes in big.Ints BigGByte = (&big.Int{}).Mul(BigMByte, bigSIExp) // BigTByte is 1,000 SI g bytes in big.Ints BigTByte = (&big.Int{}).Mul(BigGByte, bigSIExp) // BigPByte is 1,000 SI t bytes in big.Ints BigPByte = (&big.Int{}).Mul(BigTByte, bigSIExp) // BigEByte is 1,000 SI p bytes in big.Ints BigEByte = (&big.Int{}).Mul(BigPByte, bigSIExp) // BigZByte is 1,000 SI e bytes in big.Ints BigZByte = (&big.Int{}).Mul(BigEByte, bigSIExp) // BigYByte is 1,000 SI z bytes in big.Ints BigYByte = (&big.Int{}).Mul(BigZByte, bigSIExp) )
var ( // Rand goroutine-safe, use Rand.xxx instead of rand.xxx Rand = NewRand() Seed = FastRand() )
var ChinaTimeLocation = time.FixedZone(ChinaTimeZone, ChinaTimeOffset)
var ErrInvalidHostPort = errors.New("invalid Host or Port")
var StackTraceBufferSize = 4 << 10
Functions ¶
func B2S ¶
B2S 返回字节切片底层数据的零拷贝字符串视图, nil 或空切片返回空字符串. 字符串仍在使用时, 调用方不得修改或归还底层字节到对象池; 需要独立副本时使用 CopyB2S. 直接复用切片头的数据指针和长度; Go 1.27/amd64 基准中 unsafe.String 写法仍有额外检查开销.
func B64UrlEncode ¶
B64UrlEncode Base64 解码, 安全 URL, 替换: "+/" 为 "-_"
func CallPath ¶
func CallPath() string
CallPath 运行时路径, 编译目录 假如: mklink E:\tmp\linkapp.exe D:\Fufu\Test\abc\app.exe 执行: E:\tmp\linkapp.exe CallPath: E:\Go\src\github.com\fufuok\utils\tmp\osext
func CheckDomain ¶
CheckDomain returns an error if the host name is not valid. See https://tools.ietf.org/html/rfc1034#section-3.5 and https://tools.ietf.org/html/rfc1123#section-2. Ref: chmike/domain
func CopyString ¶
CopyString Immutable, string to string e.g. fiberParam := utils.CopyString(c.Params("test")) e.g. utils.CopyString(s[500:1000]) // 可以让 s 被 GC 回收 strings.Clone(s) // go1.18
func CutBytes ¶
CutBytes slices s around the first instance of sep, returning the text before and after sep. The found result reports whether sep appears in s. If sep does not appear in s, cut returns s, nil, false.
Cut returns slices of the original slice s, not copies. Ref: go1.18
func CutString ¶
CutString xslices s around the first instance of sep, returning the text before and after sep. The found result reports whether sep appears in s. If sep does not appear in s, cut returns s, "", false. Ref: go1.18
func EncodeUUID ¶
EncodeUUID 返回 36 字节的小写十六进制短横线文本, 不修改输入或验证版本及变体. 输入不足 16 字节时在末尾补零, 超出时只取前 16 字节; nil 与空切片编码为全零 UUID. 相同输入始终得到相同结果, 不受进程重启影响.
func EqualFoldBytes ¶
EqualFoldBytes tests ascii slices for equality case-insensitively Ref: fiber
func Executable ¶
Executable 当前执行程序绝对路径 true 时返回解析符号链接后的绝对路径 Excutable: E:\tmp\linkapp.exe Excutable(true): D:\Fufu\Test\abc\app.exe
func ExecutableDir ¶
ExecutableDir 当前执行程序所在目录 true 时返回解析符号链接后的目录 ExcutableDir: E:\tmp ExcutableDir(true): D:\Fufu\Test\abc
func FastIntn ¶
FastIntn this is similar to rand.Intn, but faster. A non-negative pseudo-random number in the half-open interval [0,n). Return 0 if n <= 0.
func FastRand64 ¶
func FastRand64() uint64
FastRand64 返回覆盖 uint64 全部取值范围的伪随机数, 可并发调用. 使用标准库的进程随机源, 不提供可重现序列, 不适用于密钥或安全令牌.
func FastRandn ¶
FastRandn 等同于 FastRand() % n, 但更快 See https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
func FastRandu ¶
func FastRandu() uint
FastRandu 返回覆盖 uint 全部取值范围的伪随机数, 位宽随目标平台变化, 可并发调用. 使用标准库公开入口以避免依赖已移除的 runtime.fastrandu 符号, 不适用于密码学用途.
func FirstString ¶
FirstString 获取文本内容第一个分隔符(单字节: sep[0])前的内容
func GetNotInternalIPv4 ¶
GetNotInternalIPv4 如果是内网 IPv4 则使用默认值, flag 为真是必定返回一个 IP
func GetNotInternalIPv4String ¶
GetNotInternalIPv4String 如果是内网 IPv4 则使用默认值
func GetSafeB2S ¶
GetSafeB2S Immutable, 可选指定默认值
func GetSafeBytes ¶
GetSafeBytes Immutable, 可选指定默认值
func GetSafeS2B ¶
GetSafeS2B Immutable, 可选指定默认值
func GetSafeString ¶
GetSafeString Immutable, 可选指定默认值
func HumanBaseBytes ¶
HumanBaseBytes 按指定进制格式化数量级, 最大单位不超过 sizes 的末项. 未缩放的值输出整数, 缩放后固定保留两位小数; 这是本地显示策略, 不用于无损序列化.
func HumanBigBytes ¶
HumanBigBytes produces a human readable representation of an SI size.
See also: ParseHumanBigBytes.
HumanBigBytes(82854982) -> 83 MB
func HumanBigIBytes ¶
HumanBigIBytes produces a human readable representation of an IEC size.
See also: ParseHumanBigBytes.
HumanBigIBytes(82854982) -> 79 MiB
func HumanBigKbps ¶
HumanBigKbps 1 Kbps = 1000 bit, 传输速率(bit per second, 位每秒) e.g. HumanBigKbps(82854982) -> 83 Mbps
func HumanBytes ¶
HumanBytes 1 KB = 1000 B e.g. HumanBytes(82854982) -> 82.85 MB
func HumanIBytes ¶
HumanIBytes 1 KiB = 1024 B e.g. HumanIBytes(82854982) -> 79.02 MiB
func HumanKbps ¶
HumanKbps 1 Kbps = 1000 bit, 传输速率(bit per second, 位每秒) e.g. HumanKbps(82854982) -> 82.85 Mbps
func IPv4String2LongLittle ¶
IPv4String2LongLittle IPv4 字符串转数值(小端)
func InIPNetString ¶
InIPNetString 是否包含在指定 IPNet 列表中
func InitCSTLocation ¶
InitCSTLocation 初始化默认时区为中国东八区(GMT+8) 返回值: name: "Asia/Shanghai" 或本地时区名称 loc: 优先尝试解析中国时区, 失败(Windows)后使用本地时区(time.Local) cst: 强制偏移的中国时区, !!!注意: 无法使用 time.LoadLocation(cst.String()) 二次加载 ok: true 表示初始化中国时区成功, false 表示 local 不一定是中国时区 Deprecated: 对于 公元1年 这种极早日期, 某些系统的时区数据库可能使用当时的 本地平均时间(LMT), 结果: +08:05:43 而非标准 +08:00
func InitChinaLocation ¶
InitChinaLocation 设置全局时区为中国东八区(GMT+8)
func IsIPv4 ¶
IsIPv4 判断是否为合法 IPv4 IsIPv4 works the same way as net.ParseIP, but without check for IPv6 case and without returning net.IP slice, whereby IsIPv4 makes no allocations. Ref: gofiber/utils
func IsIPv6 ¶
IsIPv6 判断是否为合法 IPv6 IsIPv6 works the same way as net.ParseIP, but without check for IPv4 case and without returning net.IP slice, whereby IsIPv6 makes no allocations. Ref: gofiber/utils
func IsInternalIPv4 ¶
IsInternalIPv4 是否为内网 IPv4, 包含 NAT 专用网段 RFC6598, 比如华为云 ELB 的 100.125.0.0/16. 100.64.0.0/10 上界是 100.127.255.255; 100.128.0.0/9 是公网, 不能当内网.
func IsInternalIPv4String ¶
IsInternalIPv4String 是否为内网 IPv4
func IsLetterOrNumeric ¶
IsLetterOrNumeric 检查字符串是否全是 ASCII 字母或数字
func IsPrivateIP ¶
IsPrivateIP reports whether ip is a private address, according to RFC 1918 (IPv4 addresses) and RFC 4193 (IPv6 addresses). Ref: go1.17+ func (ip IP) IsPrivate() bool
func JoinStringBytes ¶
JoinStringBytes 拼接字符串, 返回 bytes from bytes.Join()
func LastString ¶
LastString 返回最后一个完整 sep 之后的非空内容, 支持多字符和 UTF-8 分隔符. sep 为空, 未找到分隔符或分隔符位于末尾时, 返回首个 defaultStr; 未提供默认值则返回空字符串. 只需要最后一段及默认值时使用本函数; 需要前半段或区分未找到与空尾段时使用 strings.CutLast.
LastString("a::b", "::", "unknown") // 返回 "b".
LastString("abc", "::", "unknown") // 未找到分隔符, 返回 "unknown".
LastString("a::", "::", "unknown") // 最后一段为空, 返回 "unknown".
func LongLittle2IPv4String ¶
LongLittle2IPv4String 数值(小端)转 IPv4 字符串
func MustJSONIndentString ¶
MustJSONIndentString 转 json Indent 返回 string
func MustParseHumanBigBytes ¶
MustParseHumanBigBytes 解析数字的数量级表示 e.g. MustParseHumanBigBytes("42 MB") -> 42000000 e.g. MustParseHumanBigBytes("-42 mib", 123) -> 123
func MustParseHumanBytes ¶
MustParseHumanBytes 解析数字的数量级表示 e.g. MustParseHumanBytes("42 MB") -> 42000000 e.g. MustParseHumanBytes("-42 mib", 123) -> 123
func ParseHostPort ¶
ParseHostPort 解析 IP 和端口
func ParseHumanBigBytes ¶
ParseHumanBigBytes parses a string representation of bytes into the number of bytes it represents.
See also: HumanBigBytes, HumanBigIBytes.
ParseHumanBigBytes("42 MB") -> 42000000, nil ParseHumanBigBytes("42 mib") -> 44040192, nil
func ParseHumanBytes ¶
ParseHumanBytes 解析数字的数量级表示, 整数输入在 uint64 范围内精确换算. 小数沿用 float64 换算并截去不足一字节的部分, 非法单位或溢出返回错误. e.g. ParseHumanBytes("42 MB") -> 42000000, nil e.g. ParseHumanBytes("42 mib") -> 44040192, nil
func ParseIPxWithNumeric ¶
ParseIPxWithNumeric 解析 IP, 支持数字形态, 并返回是否为 IPv6
func RandAlphaBytes ¶
RandAlphaBytes generates random alpha bytes.
func RandBytesLetters ¶
RandBytesLetters 生成指定长度的字符切片
func RandHexBytes ¶
RandHexBytes generates random hexadecimal bytes.
func RandString ¶
RandString a random string, which may contain uppercase letters, lowercase letters and numbers. Ref: stackoverflow.icza
func ReplaceHost ¶
ReplaceHost 返回 b 的主机名 + a 的端口 e.g. ReplaceHost("a.cn:77", "b.cn:88") == "b.cn:77"
func Round ¶
Round 四舍五入, ROUND_HALF_UP 模式实现 返回将 val 根据指定精度 precision (十进制小数点后数字的数目) 进行四舍五入的结果 precision 也可以是负数或零 Ref: thinkeridea/go-extend
func RuneSubString ¶
RuneSubString 多字节字符串截取 长度支持 0(空字符串) / >0(正常截取) / <0(从结尾处往前截取) 长度等于或超过字符串长度, 返回原字符串值, 此时忽略 suffix 参数
func S2B ¶
S2B 返回字符串底层数据的零拷贝字节视图, 长度和容量均等于字符串长度. 返回切片只可读取, 不得修改; 需要可写的独立副本时使用 CopyS2B. 空字符串返回零长度切片, 是否为 nil 由字符串的底层表示决定.
func SafeGoCommonFunc ¶
func SafeGoCommonFunc(args any, fn func(args any), cb ...RecoveryCallback)
SafeGoCommonFunc 带 Recover 的 goroutine 运行
func SafeGoWithContext ¶
func SafeGoWithContext(ctx context.Context, fn func(ctx context.Context), cb ...RecoveryCallback)
SafeGoWithContext 带 Recover 的 goroutine 运行
func SearchString ¶
SearchString 搜索字符串位置(左, 第一个) 用 slices.Index 替代
func SplitHostPort ¶
SplitHostPort separates host and port. If the port is not valid, it returns the entire input as host, and it doesn't check the validity of the host. Unlike net.SplitHostPort, but per RFC 3986, it requires ports to be numeric.
func ToLowerBytes ¶
ToLowerBytes converts ascii slice to lower-case Ref: fiber
func ToUpperBytes ¶
ToUpperBytes converts ascii slice to upper-case Ref: fiber
func TrimBOMSpace ¶ added in v1.3.3
TrimBOMSpace 去掉字符串首尾的 UTF-8 BOM (U+FEFF) 和空白. U+FEFF 的 Unicode 分类是 Format, unicode.IsSpace 为 false, strings.TrimSpace 去不掉. BOM 和空白可能交替出现, 例如 " \uFEFF value \uFEFF ", 所以交替清除到稳定. 只处理首尾, 不删除字符串内部的 BOM 或空白.
func TrimLeftBytes ¶
TrimLeftBytes is the equivalent of bytes.TrimLeft Ref: fiber
func TrimRightBytes ¶
TrimRightBytes is the equivalent of bytes.TrimRight Ref: fiber
func TruncStrTail ¶ added in v1.3.4
TruncStrTail 按 Unicode 字符从尾部保留字符串, 供命令输出保留最终结果. suffix 放在保留内容前面; maxLen 小于等于 0 或输入为空时返回空字符串.
func UUID ¶
func UUID() []byte
UUID 返回独立可写的 16 字节随机 UUID v4, 符合 RFC 9562. 标准库使用密码学随机源生成 122 位随机数据; 多次调用及重启后的结果不可复现.
func ValidOptionalPort ¶
ValidOptionalPort reports whether port is either an empty string or matches /^:\d*$/
func WaitNextMinuteWithTime ¶
WaitNextMinuteWithTime 下一分钟, 对齐时间, 0 秒
func WaitNextSecondWithTime ¶
WaitNextSecondWithTime 下一秒, 对齐时间, 0 毫秒 (近似)
Types ¶
type Bool ¶
type Bool struct {
// contains filtered or unexported fields
}
A Bool is an atomic boolean value. The zero value is false.
func (*Bool) CompareAndSwap ¶
CompareAndSwap executes the compare-and-swap operation for the boolean value x.
func (*Bool) MarshalJSON ¶
func (*Bool) StoreFalse ¶
func (x *Bool) StoreFalse()
func (*Bool) UnmarshalJSON ¶
type NoCmp ¶
type NoCmp [0]func()
NoCmp is an uncomparable struct. Embed this inside another struct to make it uncomparable.
type Foo struct {
NoCmp
// ...
}
This DOES NOT:
- Disallow shallow copies of structs
- Disallow comparison of pointers to uncomparable structs
type NoCopy ¶
type NoCopy struct{} //nolint:unused
NoCopy may be added to structs which must not be copied after the first use.
See https://github.com/golang/go/issues/8005#issuecomment-190753527 for details. and also: https://stackoverflow.com/questions/52494458/nocopy-minimal-example
Note that it must not be embedded, due to the Lock and Unlock methods.
type RecoveryCallback ¶
RecoveryCallback 自定义恢复信息回调