Skip to content

Latest commit

 

History

History
2403 lines (1671 loc) · 51.6 KB

DOC.md

File metadata and controls

2403 lines (1671 loc) · 51.6 KB

utils

import "github.com/fufuok/utils"

Index

Constants

Ref: dustin/go-humanize IEC Sizes. kibis of bits

const (
    Byte = 1 << (iota * 10)
    KiByte
    MiByte
    GiByte
    TiByte
    PiByte
    EiByte
)

SI Sizes.

const (
    IByte = 1
    KByte = IByte * 1000
    MByte = KByte * 1000
    GByte = MByte * 1000
    TByte = GByte * 1000
    PByte = TByte * 1000
    EByte = PByte * 1000
)

const (
    IPv4Min = 0
    IPv4Max = 1<<32 - 1
)

const (
    // PtrSize 4 on 32-bit systems, 8 on 64-bit.
    PtrSize = 4 << (^uintptr(0) >> 63)
)

Variables

Ref: dustin/go-humanize

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)
)

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 ErrInvalidHostPort = errors.New("invalid Host or Port")

var StackTraceBufferSize = 4 << 10

func B2S

func B2S(b []byte) string

B2S converts byte slice to string without a memory allocation. Slower: unsafe.String(unsafe.SliceData(b), len(b)) strings.Clone(): unsafe.String(&b[0], len(b))

func B64Decode

func B64Decode(s string) []byte

B64Decode Base64 解码

func B64Encode

func B64Encode(b []byte) string

B64Encode Base64 编码

func B64UrlDecode

func B64UrlDecode(s string) []byte

B64UrlDecode Base64 解码

func B64UrlEncode

func B64UrlEncode(b []byte) string

B64UrlEncode Base64 解码, 安全 URL, 替换: "+/" 为 "-_"

func BeginOfDay

func BeginOfDay(t time.Time) time.Time

BeginOfDay 当天 0 点

func BeginOfHour

func BeginOfHour(t time.Time) time.Time

BeginOfHour 0 分

func BeginOfLastMonth

func BeginOfLastMonth(t time.Time) time.Time

BeginOfLastMonth 上月第一天 0 点

func BeginOfLastWeek

func BeginOfLastWeek(t time.Time) time.Time

BeginOfLastWeek 上周一 0 点

func BeginOfMinute

func BeginOfMinute(t time.Time) time.Time

BeginOfMinute 0 秒

func BeginOfMonth

func BeginOfMonth(t time.Time) time.Time

BeginOfMonth 当月第一天 0 点

func BeginOfNextMonth

func BeginOfNextMonth(t time.Time) time.Time

BeginOfNextMonth 下月第一天 0 点

func BeginOfNextWeek

func BeginOfNextWeek(t time.Time) time.Time

BeginOfNextWeek 下周一 0 点

func BeginOfSecond

func BeginOfSecond(t time.Time) time.Time

BeginOfSecond 0 毫秒

func BeginOfTomorrow

func BeginOfTomorrow(t time.Time) time.Time

BeginOfTomorrow 明天 0 点

func BeginOfWeek

func BeginOfWeek(t time.Time) time.Time

BeginOfWeek 本周一 0 点

func BeginOfYear

func BeginOfYear(t time.Time) time.Time

BeginOfYear 本年第一天 0 点

func BeginOfYesterday

func BeginOfYesterday(t time.Time) time.Time

BeginOfYesterday 昨天 0 点

func BigComma

func BigComma(b *big.Int) string

BigComma big.Int 千分位分隔字符串 Ref: dustin/go-humanize

func BigCommaf

func BigCommaf(v *big.Float) string

BigCommaf big.Float 千分位分隔字符串 Ref: dustin/go-humanize

func Bigoom

func Bigoom(n, b *big.Int) (float64, int)

Bigoom big.Int 总数量级 Ref: dustin/go-humanize

func CPUTicks

func CPUTicks() int64

CPUTicks CPU 时钟周期, 更高精度 (云服务器做伪随机数种子时慎用)

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 Comma

func Comma(v int64) string

Comma 整数转千分位分隔字符串 Ref: dustin/go-humanize e.g. Comma(834142) -> 834,142

func Commaf

func Commaf(v float64) string

Commaf 浮点数转千分位分隔字符串 Ref: dustin/go-humanize e.g. Commaf(834142.32) -> 834,142.32

func Commai

func Commai(v int) string

Commai 整数转千分位分隔字符串

func Commau

func Commau(v uint64) string

Commau 整数转千分位分隔字符串 Ref: dustin/go-humanize

func CopyB2S

func CopyB2S(b []byte) string

CopyB2S Immutable, []byte to string string(b)

func CopyBytes

func CopyBytes(b []byte) []byte

CopyBytes Immutable, []byte to []byte

func CopyS2B

func CopyS2B(s string) []byte

CopyS2B Immutable, string to []byte []byte(s)

func CopyString

func CopyString(s string) string

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

func CutBytes(s, sep []byte) (before, after []byte, found bool)

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

func CutString(s, sep string) (before, after string, found bool)

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 DaysInMonth

func DaysInMonth(year int, m time.Month) int

DaysInMonth 返回月份天数

func DaysInYear

func DaysInYear(year int) int

DaysInYear 返回年份天数

func EncodeUUID

func EncodeUUID(id []byte) []byte

EncodeUUID 编码 UUID

func EndOfDay

func EndOfDay(t time.Time) time.Time

EndOfDay 当天最后时刻

func EndOfHour

func EndOfHour(t time.Time) time.Time

EndOfHour 最后一分

func EndOfLastMonth

func EndOfLastMonth(t time.Time) time.Time

EndOfLastMonth 上月最后一刻

func EndOfLastWeek

func EndOfLastWeek(t time.Time) time.Time

EndOfLastWeek 上周一最后一刻

func EndOfMinute

func EndOfMinute(t time.Time) time.Time

EndOfMinute 最后一秒

func EndOfMonth

func EndOfMonth(t time.Time) time.Time

EndOfMonth 当月最后一刻

func EndOfNextMonth

func EndOfNextMonth(t time.Time) time.Time

EndOfNextMonth 下月最后一刻

func EndOfNextWeek

func EndOfNextWeek(t time.Time) time.Time

EndOfNextWeek 下周一最后一刻

func EndOfSecond

func EndOfSecond(t time.Time) time.Time

EndOfSecond 最后一毫秒

func EndOfTomorrow

func EndOfTomorrow(t time.Time) time.Time

EndOfTomorrow 明天 0 点

func EndOfWeek

func EndOfWeek(t time.Time) time.Time

EndOfWeek 本周末最后一刻

func EndOfYear

func EndOfYear(t time.Time) time.Time

EndOfYear 本年最后一刻

func EndOfYesterday

func EndOfYesterday(t time.Time) time.Time

EndOfYesterday 昨天最后时刻

func EqualFold

func EqualFold(b, s string) bool

EqualFold tests ascii strings for equality case-insensitively Ref: fiber

func EqualFoldBytes

func EqualFoldBytes(b, s []byte) bool

EqualFoldBytes tests ascii slices for equality case-insensitively Ref: fiber

func Executable

func Executable(evalSymlinks ...bool) string

Executable 当前执行程序绝对路径 true 时返回解析符号链接后的绝对路径 Excutable: E:\tmp\linkapp.exe Excutable(true): D:\Fufu\Test\abc\app.exe

func ExecutableDir

func ExecutableDir(evalSymlinks ...bool) string

ExecutableDir 当前执行程序所在目录 true 时返回解析符号链接后的目录 ExcutableDir: E:\tmp ExcutableDir(true): D:\Fufu\Test\abc

func FastIntn

func FastIntn(n int) int

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 FastRand

func FastRand() uint32

FastRand 随机数

func FastRand64

func FastRand64() uint64

func FastRandn

func FastRandn(n uint32) uint32

FastRandn 等同于 FastRand() % n, 但更快 See https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/

func FastRandu

func FastRandu() uint

func GetBytes

func GetBytes(v interface{}, defaultVal ...[]byte) []byte

GetBytes 先转为字符串再转为 []byte, 可选指定默认值

func GetIPPort

func GetIPPort(addr net.Addr) (ip net.IP, port int, err error)

GetIPPort 返回 IP 和 端口

func GetInt

func GetInt(v interface{}, defaultInt ...int) int

GetInt 获取 int 结果, 可选指定默认值(若给定了默认值,则返回正整数或 0)

func GetMonthDays

func GetMonthDays(t time.Time) int

GetMonthDays 当月天数

func GetNotInternalIPv4

func GetNotInternalIPv4(ip, defaultIP net.IP, flag ...bool) net.IP

GetNotInternalIPv4 如果是内网 IPv4 则使用默认值, flag 为真是必定返回一个 IP

func GetNotInternalIPv4String

func GetNotInternalIPv4String(ip, defaultIP string, flag ...bool) string

GetNotInternalIPv4String 如果是内网 IPv4 则使用默认值

func GetSafeB2S

func GetSafeB2S(b []byte, defaultVal ...string) string

GetSafeB2S Immutable, 可选指定默认值

func GetSafeBytes

func GetSafeBytes(b []byte, defaultVal ...[]byte) []byte

GetSafeBytes Immutable, 可选指定默认值

func GetSafeS2B

func GetSafeS2B(s string, defaultVal ...[]byte) []byte

GetSafeS2B Immutable, 可选指定默认值

func GetSafeString

func GetSafeString(s string, defaultVal ...string) string

GetSafeString Immutable, 可选指定默认值

func GetString

func GetString(v interface{}, defaultVal ...string) string

GetString 获取字符串结果, 可选指定默认值

func GoroutineID

func GoroutineID() (uint64, error)

GoroutineID 获取 Goroutine ID

func Gzip

func Gzip(data []byte) ([]byte, error)

func GzipLevel

func GzipLevel(data []byte, level int) (dst []byte, err error)

func HumanBaseBytes

func HumanBaseBytes(v uint64, base float64, sizes []string) string

HumanBaseBytes 数字的数量级表示

func HumanBigBytes

func HumanBigBytes(s *big.Int) string

HumanBigBytes produces a human readable representation of an SI size.

See also: ParseHumanBigBytes.

HumanBigBytes(82854982) -> 83 MB

func HumanBigIBytes

func HumanBigIBytes(s *big.Int) string

HumanBigIBytes produces a human readable representation of an IEC size.

See also: ParseHumanBigBytes.

HumanBigIBytes(82854982) -> 79 MiB

func HumanBigKbps

func HumanBigKbps(s *big.Int) string

HumanBigKbps 1 Kbps = 1000 bit, 传输速率(bit per second, 位每秒) e.g. HumanBigKbps(82854982) -> 83 Mbps

func HumanBytes

func HumanBytes(v uint64) string

HumanBytes 1 KB = 1000 B e.g. HumanBytes(82854982) -> 83 MB

func HumanGBMB

func HumanGBMB(v uint64) string

HumanGBMB 转为 ** GB ** MB 1 GB = 1024 MB

func HumanIBytes

func HumanIBytes(v uint64) string

HumanIBytes 1 KiB = 1024 B e.g. HumanIBytes(82854982) -> 79 MiB

func HumanIntBytes

func HumanIntBytes(v int) string

HumanIntBytes 1 KB = 1000 B

func HumanIntIBytes

func HumanIntIBytes(v int) string

HumanIntIBytes 1 KiB = 1024 B

func HumanIntKbps

func HumanIntKbps(v int) string

HumanIntKbps 1 Kbps = 1000 bit

func HumanKbps

func HumanKbps(v uint64) string

HumanKbps 1 Kbps = 1000 bit, 传输速率(bit per second, 位每秒) e.g. HumanKbps(82854982) -> 83 Mbps

func ID

func ID() uint64

ID 运行时自增 ID (每次程序启动从 1 开始)

func IPv42Long

func IPv42Long(ip net.IP) int

IPv42Long IPv4 转数值

func IPv42LongLittle

func IPv42LongLittle(ip net.IP) int

IPv42LongLittle IPv4 转小端数值

func IPv4String2Long

func IPv4String2Long(ip string) int

IPv4String2Long IPv4 字符串转数值

func IPv4String2LongLittle

func IPv4String2LongLittle(ip string) int

IPv4String2LongLittle IPv4 字符串转数值(小端)

func IPv62Int

func IPv62Int(ip net.IP) *big.Int

IPv62Int IPv6 转数值

func IPv6String2Int

func IPv6String2Int(ip string) *big.Int

IPv6String2Int IPv6 字符串转数值

func InIPNet

func InIPNet(ip net.IP, ipNets map[*net.IPNet]struct{}) bool

InIPNet 是否包含在指定 IPNet 列表中

func InIPNetString

func InIPNetString(ip string, ipNets map[*net.IPNet]struct{}) bool

InIPNetString 是否包含在指定 IPNet 列表中

func InInts

func InInts(slice []int, n int) bool

InInts 检查整数是否存在于 slice

func InStrings

func InStrings(ss []string, s string) bool

InStrings 检查字符串是否存在于 slice 用 slices.Contains 替代

func InitCSTLocation

func InitCSTLocation() (name string, loc *time.Location, cst *time.Location, ok bool)

InitCSTLocation 初始化默认时区为中国东八区(GMT+8) 返回值: name: "Asia/Shanghai" 或本地时区名称 loc: 优先尝试解析中国时区, 失败(Windows)后使用本地时区(time.Local) cst: 强制偏移的中国时区, !!!注意: 无法使用 time.LoadLocation(cst.String()) 二次加载 ok: true 表示初始化中国时区成功, false 表示 local 不一定是中国时区

func InitLocation

func InitLocation(name string) (*time.Location, bool)

InitLocation 解析并初始化本地时区

func Int2IPv6

func Int2IPv6(ipInt *big.Int) net.IP

Int2IPv6 数值转 IPv4

func Int2IPv6String

func Int2IPv6String(n *big.Int) string

Int2IPv6String 数值转 IPv6 字符串

func IsIP

func IsIP(ip string) bool

IsIP 判断是否为合法 IPv4 / IPv6

func IsIPv4

func IsIPv4(s string) bool

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

func IsIPv6(s string) bool

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

func IsInternalIPv4(ip net.IP) bool

IsInternalIPv4 是否为内网 IPv4, 包含 NAT 专用网段 RFC6598, 比如华为云 ELB 的 100.125.0.0/16

func IsInternalIPv4String

func IsInternalIPv4String(ip string) bool

IsInternalIPv4String 是否为内网 IPv4

func IsLeapYear

func IsLeapYear(year int) bool

IsLeapYear 判断是否为闰年

func IsNumeric

func IsNumeric(s string) bool

IsNumeric 检查字符串是否全是数字: 0-9

func IsPrivateIP

func IsPrivateIP(ip net.IP) bool

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 IsPrivateIPString

func IsPrivateIPString(ip string) bool

IsPrivateIPString 是否为私有 IP

func JoinBytes

func JoinBytes(b ...[]byte) []byte

JoinBytes 拼接 []byte

func JoinString

func JoinString(s ...string) string

JoinString 拼接字符串

func JoinStringBytes

func JoinStringBytes(s ...string) []byte

JoinStringBytes 拼接字符串, 返回 bytes from bytes.Join()

func LeftPad

func LeftPad(s, pad string, n int) string

LeftPad 从左填充字符串到指定长度

func LeftPadBytes

func LeftPadBytes(b, pad []byte, n int) []byte

LeftPadBytes 从左填充到指定长度

func Logn

func Logn(n, b float64) float64

func Long2IPv4

func Long2IPv4(n int) net.IP

Long2IPv4 数值转 IPv4

func Long2IPv4String

func Long2IPv4String(n int) string

Long2IPv4String 数值转 IPv4 字符串

func LongLittle2IPv4

func LongLittle2IPv4(n int) net.IP

LongLittle2IPv4 小端数值转 IPv4

func LongLittle2IPv4String

func LongLittle2IPv4String(n int) string

LongLittle2IPv4String 数值(小端)转 IPv4 字符串

func MaxInt

func MaxInt(a, b int) int

MaxInt 整数取大值

func MinInt

func MinInt(a, b int) int

MinInt 整数取小值

func MustBool

func MustBool(v interface{}) bool

MustBool 强制转为 bool

func MustInt

func MustInt(v interface{}) int

MustInt 强制转为整数 (int)

func MustJSON

func MustJSON(v interface{}) []byte

MustJSON 转 json 返回 []byte

func MustJSONIndent

func MustJSONIndent(v interface{}) []byte

MustJSONIndent 转 json 返回 []byte

func MustJSONIndentString

func MustJSONIndentString(v interface{}) string

MustJSONIndentString 转 json Indent 返回 string

func MustJSONString

func MustJSONString(v interface{}) string

MustJSONString 转 json 返回 string

func MustParseHumanBigBytes

func MustParseHumanBigBytes(s string, defaultVal ...*big.Int) *big.Int

MustParseHumanBigBytes 解析数字的数量级表示 e.g. MustParseHumanBigBytes("42 MB") -> 42000000 e.g. MustParseHumanBigBytes("-42 mib", 123) -> 123

func MustParseHumanBytes

func MustParseHumanBytes(s string, defaultVal ...uint64) uint64

MustParseHumanBytes 解析数字的数量级表示 e.g. MustParseHumanBytes("42 MB") -> 42000000 e.g. MustParseHumanBytes("-42 mib", 123) -> 123

func MustString

func MustString(v interface{}, timeLayout ...string) string

MustString 强制转为字符串

func NanoTime

func NanoTime() int64

NanoTime 返回当前时间 (以纳秒为单位)

func NewRand

func NewRand(seed ...int64) *rand.Rand

NewRand goroutine-safe rand.Rand, optional seed value

func Pad

func Pad(s, pad string, n int) string

Pad 填充字符串到指定长度, 同 Python3: 'str'.center()

func PadBytes

func PadBytes(s, pad []byte, n int) []byte

PadBytes 填充到指定长度

func ParseHostPort

func ParseHostPort(s string) (net.IP, uint16, bool, error)

ParseHostPort 解析 IP 和端口

func ParseHumanBigBytes

func ParseHumanBigBytes(s string) (*big.Int, error)

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

func ParseHumanBytes(s string) (uint64, error)

ParseHumanBytes 解析数字的数量级表示 e.g. ParseHumanBytes("42 MB") -> 42000000, nil e.g. ParseHumanBytes("42 mib") -> 44040192, nil

func ParseIP

func ParseIP(s string) (net.IP, bool)

ParseIP 解析 IP 并返回是否为 IPv6

func ParseIPv4

func ParseIPv4(ip string) net.IP

ParseIPv4 判断是否为合法 IPv4 并解析

func ParseIPv6

func ParseIPv6(ip string) net.IP

ParseIPv6 判断是否为合法 IPv6 并解析

func ParseIPx

func ParseIPx(s string) (net.IP, bool)

ParseIPx 解析 IP, 并返回是否为 IPv6

func ParseIPxWithNumeric

func ParseIPxWithNumeric(s string) (net.IP, bool)

ParseIPxWithNumeric 解析 IP, 支持数字形态, 并返回是否为 IPv6

func ParseInts

func ParseInts(s string) ([]int, error)

ParseInts 解析字符串, 得到去重并排序后的数字列表 "5,0-3, 3" => [0,1,2,3,5]

func RandAlphaBytes

func RandAlphaBytes(n int) []byte

RandAlphaBytes generates random alpha bytes.

func RandAlphaString

func RandAlphaString(n int) string

RandAlphaString 指定长度的随机字母字符串

func RandBytes

func RandBytes(n int) []byte

RandBytes random bytes, but faster.

func RandBytesLetters

func RandBytesLetters(n int, letters string) []byte

RandBytesLetters 生成指定长度的字符切片

func RandDecBytes

func RandDecBytes(n int) []byte

RandDecBytes 指定长度的随机数字切片

func RandDecString

func RandDecString(n int) string

RandDecString 指定长度的随机数字字符串

func RandHexBytes

func RandHexBytes(n int) []byte

RandHexBytes generates random hexadecimal bytes.

func RandHexString

func RandHexString(n int) string

RandHexString 指定长度的随机 hex 字符串

func RandInt

func RandInt(min, max int) int

RandInt (>=)min - (<)max

func RandString

func RandString(n int) string

RandString a random string, which may contain uppercase letters, lowercase letters and numbers. Ref: stackoverflow.icza

func RandUint32

func RandUint32(min, max uint32) uint32

RandUint32 (>=)min - (<)max

func Recover

func Recover(cb ...RecoveryCallback)

Recover 从 panic 中恢复并记录堆栈信息

func RemoveString

func RemoveString(ss []string, s string) ([]string, bool)

RemoveString 删除字符串元素

func ReplaceHost

func ReplaceHost(a, b string) string

ReplaceHost 返回 b 的主机名 + a 的端口 e.g. ReplaceHost("a.cn:77", "b.cn:88") == "b.cn:77"

func Reverse

func Reverse(s string) string

Reverse 反转字符串

func RightPad

func RightPad(s, pad string, n int) string

RightPad 从右填充字符串到指定长度

func RightPadBytes

func RightPadBytes(b, pad []byte, n int) []byte

RightPadBytes 从右填充到指定长度

func Round

func Round(v float64, precision int) float64

Round 四舍五入, ROUND_HALF_UP 模式实现 返回将 val 根据指定精度 precision (十进制小数点后数字的数目) 进行四舍五入的结果 precision 也可以是负数或零 Ref: thinkeridea/go-extend

func RunPath

func RunPath() string

RunPath 实际程序所在目录 RunPath: E:\tmp

func RuneReverse

func RuneReverse(s string) string

RuneReverse 反转多字节字符串

func RuneSubString

func RuneSubString(s string, length int, suffix string) string

RuneSubString 多字节字符串截取 长度支持 0(空字符串) / >0(正常截取) / <0(从结尾处往前截取) 长度等于或超过字符串长度, 返回原字符串值, 此时忽略 suffix 参数

func S2B

func S2B(s string) []byte

S2B converts string to byte slice without a memory allocation. Ref: golang/go#53003 (comment)

func SafeGo

func SafeGo(fn func(), cb ...RecoveryCallback)

SafeGo 带 Recover 的 goroutine 运行

func SafeGoCommonFunc

func SafeGoCommonFunc(args interface{}, fn func(args interface{}), cb ...RecoveryCallback)

SafeGoCommonFunc 带 Recover 的 goroutine 运行

func SafeGoWithContext

func SafeGoWithContext(ctx context.Context, fn func(ctx context.Context), cb ...RecoveryCallback)

SafeGoWithContext 带 Recover 的 goroutine 运行

func SearchInt

func SearchInt(slice []int, n int) int

SearchInt 搜索整数位置(左, 第一个)

func SearchString

func SearchString(ss []string, s string) int

SearchString 搜索字符串位置(左, 第一个) 用 slices.Index 替代

func Sleep

func Sleep(ctx context.Context, interval time.Duration) error

Sleep 支持上下文中断的 time.Sleep

func SplitHostPort

func SplitHostPort(hostPort string) (host, port string)

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 SumInt

func SumInt(v ...int) int

SumInt 整数和

func ToLower

func ToLower(b string) string

ToLower converts ascii string to lower-case Ref: fiber

func ToLowerBytes

func ToLowerBytes(b []byte) []byte

ToLowerBytes converts ascii slice to lower-case Ref: fiber

func ToUpper

func ToUpper(b string) string

ToUpper converts ascii string to upper-case Ref: fiber

func ToUpperBytes

func ToUpperBytes(b []byte) []byte

ToUpperBytes converts ascii slice to upper-case Ref: fiber

func Trim

func Trim(s string, cutset byte) string

Trim is the equivalent of strings.Trim Ref: fiber

func TrimBytes

func TrimBytes(b []byte, cutset byte) []byte

TrimBytes is the equivalent of bytes.Trim Ref: fiber

func TrimLeft

func TrimLeft(s string, cutset byte) string

TrimLeft is the equivalent of strings.TrimLeft Ref: fiber

func TrimLeftBytes

func TrimLeftBytes(b []byte, cutset byte) []byte

TrimLeftBytes is the equivalent of bytes.TrimLeft Ref: fiber

func TrimRight

func TrimRight(s string, cutset byte) string

TrimRight is the equivalent of strings.TrimRight Ref: fiber

func TrimRightBytes

func TrimRightBytes(b []byte, cutset byte) []byte

TrimRightBytes is the equivalent of bytes.TrimRight Ref: fiber

func TrimSlice

func TrimSlice(ss []string) []string

TrimSlice 清除 slice 中各元素的空白, 并删除空白项 注意: 原切片将被修改

func UUID

func UUID() []byte

UUID 随机 UUID, RFC4122, Version 4

func UUIDShort

func UUIDShort() string

UUIDShort 随机 UUID, 短版, base58

func UUIDSimple

func UUIDSimple() string

UUIDSimple 随机 UUID, 无短横线

func UUIDString

func UUIDString() string

UUIDString 随机 UUID

func Ungzip

func Ungzip(data []byte) (src []byte, err error)

func Unzip

func Unzip(data []byte) (src []byte, err error)

func ValidOptionalPort

func ValidOptionalPort(port string) bool

ValidOptionalPort reports whether port is either an empty string or matches /^:\d*$/

func WaitNextMinute

func WaitNextMinute(t ...time.Time)

WaitNextMinute 下一分钟, 对齐时间, 0 秒

func WaitNextMinuteWithTime

func WaitNextMinuteWithTime(t ...time.Time) (now time.Time)

WaitNextMinuteWithTime 下一分钟, 对齐时间, 0 秒

func WaitNextSecond

func WaitNextSecond(t ...time.Time)

WaitNextSecond 下一秒, 对齐时间, 0 毫秒 (近似)

func WaitNextSecondWithTime

func WaitNextSecondWithTime(t ...time.Time) (now time.Time)

WaitNextSecondWithTime 下一秒, 对齐时间, 0 毫秒 (近似)

func WaitSignal

func WaitSignal(sig ...os.Signal) os.Signal

WaitSignal 等待系统信号 默认捕获退出类信息

func WaitUntilMinute

func WaitUntilMinute(m int, t ...time.Time)

WaitUntilMinute 等待, 直到 m 分钟

func WaitUntilSecond

func WaitUntilSecond(s int, t ...time.Time)

WaitUntilSecond 等待, 直到 s 秒

func Zip

func Zip(data []byte) ([]byte, error)

func ZipLevel

func ZipLevel(data []byte, level int) (dst []byte, err error)

type Bool

A Bool is an atomic boolean value. The zero value is false.

type Bool struct {
    // contains filtered or unexported fields
}

func NewBool

func NewBool(val bool) *Bool

func NewFalse

func NewFalse() *Bool

func NewTrue

func NewTrue() *Bool

func (*Bool) CAS

func (x *Bool) CAS(old, new bool) bool

func (*Bool) CompareAndSwap

func (x *Bool) CompareAndSwap(old, new bool) (swapped bool)

CompareAndSwap executes the compare-and-swap operation for the boolean value x.

func (*Bool) Load

func (x *Bool) Load() bool

Load atomically loads and returns the value stored in x.

func (*Bool) MarshalJSON

func (x *Bool) MarshalJSON() ([]byte, error)

func (*Bool) Store

func (x *Bool) Store(val bool)

Store atomically stores val into x.

func (*Bool) StoreFalse

func (x *Bool) StoreFalse()

func (*Bool) StoreTrue

func (x *Bool) StoreTrue()

func (*Bool) String

func (x *Bool) String() string

func (*Bool) Swap

func (x *Bool) Swap(new bool) (old bool)

Swap atomically stores new into x and returns the previous value.

func (*Bool) Toggle

func (x *Bool) Toggle() (old bool)

Toggle atomically negates the Boolean and returns the previous value

func (*Bool) UnmarshalJSON

func (x *Bool) UnmarshalJSON(b []byte) error

type NoCmp

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 NoCmp [0]func()

type NoCopy

NoCopy may be added to structs which must not be copied after the first use.

See golang/go#8005 (comment) 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 NoCopy struct{} //nolint:unused

func (*NoCopy) Lock

func (*NoCopy) Lock()

Lock is a no-op used by -copylocks checker from `go vet`.

func (*NoCopy) Unlock

func (*NoCopy) Unlock()

type RecoveryCallback

RecoveryCallback 自定义恢复信息回调

type RecoveryCallback func(err interface{}, trace []byte)

type TryMutex

type TryMutex struct {
    // contains filtered or unexported fields
}

func NewTryMutex

func NewTryMutex() *TryMutex

func (*TryMutex) Lock

func (m *TryMutex) Lock()

func (*TryMutex) TryLock

func (m *TryMutex) TryLock(timeout ...time.Duration) bool

TryLock 实现可选等待时间尝试获取锁

func (*TryMutex) Unlock

func (m *TryMutex) Unlock()

Generated by gomarkdoc