util

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2023 License: MIT Imports: 24 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Rand goroutine-safe, use Rand.xxx instead of rand.xxx
	Rand = NewRand()
	Seed = FastRand()
)
View Source
var (
	StackTraceBufferSize = 4 << 10
)

Functions

func B2S

func B2S(b []byte) string

B2S BytesToString

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 CPUTicks

func CPUTicks() int64

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

func CSTLayoutString

func CSTLayoutString() string

CSTLayoutString 格式化时间 返回 "2006-01-02 15:04:05" 格式的时间

func CSTLayoutStringToUnix

func CSTLayoutStringToUnix(cstLayoutString string) (int64, error)

CSTLayoutStringToUnix 返回 unix 时间戳 2020-01-24 21:11:11 => 1579871471

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 DaysInYear

func DaysInYear(year int) int

DaysInYear 返回年份天数

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

func Extract(hostPort string, lis net.Listener) (string, error)

Extract returns a private addr and port.

func ExtractHostPort

func ExtractHostPort(addr string) (host string, port uint64, err error)

ExtractHostPort from address

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 FastRandBytes

func FastRandBytes(n int) []byte

FastRandBytes random bytes, but faster.

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 FormatAmount

func FormatAmount(amount float64) int

FormatAmount 格式化金额为分,入库保存

func FormatResUrl

func FormatResUrl(dfs, url string) string

FormatResUrl 格式化图片资源完整路劲

func GMTLayoutString

func GMTLayoutString() string

GMTLayoutString 格式化时间 返回 "Mon, 02 Jan 2006 15:04:05 GMT" 格式的时间

func GetMonthDays

func GetMonthDays(t time.Time) int

GetMonthDays 当月天数

func GetRealIP

func GetRealIP(r *http.Request) (ip string)

GetRealIP get user real ip

func Hostname

func Hostname() string

Hostname 获取主机名

func InSliceInt

func InSliceInt(s int, ss []int) bool

InSliceInt 是否在 slice

func InSliceStr

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

InSliceStr 是否在 slice

func InterfaceAddrs

func InterfaceAddrs(v ...string) (map[string][]net.IP, error)

InterfaceAddrs 获取所有带 IP 的接口和对应的所有 IP 排除本地链路地址和环回地址

func InternalIP

func InternalIP(dstAddr, network string) string

InternalIP 获取内网地址 (出口本地地址)

func InternalIPv4

func InternalIPv4() string

InternalIPv4 获取内网地址 (IPv4)

func InternalIPv6

func InternalIPv6() string

InternalIPv6 获取内网地址 (临时 IPv6 地址)

func IsLeapYear

func IsLeapYear(year int) bool

IsLeapYear 判断是否为闰年

func JoinBytes

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

JoinBytes 拼接 []byte

func LeftPad

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

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

func LeftPadBytes

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

LeftPadBytes 从左填充到指定长度

func LocalIP

func LocalIP() string

LocalIP 获取本地地址 (第一个)

func LocalIPv4s

func LocalIPv4s() (ips []string)

LocalIPv4s 获取所有本地地址 IPv4

func MapBuildQuery added in v0.0.10

func MapBuildQuery(m map[string]any) string

MapBuildQuery map => url query

func MustInt

func MustInt(v any) int

MustInt 强制转为整数 (int)

func MustString

func MustString(v any, 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 ParseAmount

func ParseAmount(amount int) float64

ParseAmount 金额分转元,保留两位小数输出

func ParseCSTInLocation

func ParseCSTInLocation(date string) (time.Time, error)

ParseCSTInLocation 格式化时间

func ParseGMTInLocation

func ParseGMTInLocation(date string) (time.Time, error)

ParseGMTInLocation 格式化时间

func Port

func Port(lis net.Listener) (int, bool)

Port return a real port.

func RandBytes

func RandBytes(n int) ([]byte, error)

RandBytes returns n random bytes

func RandHex

func RandHex(nHalf int) string

RandHex a random string containing only the following characters: 0123456789abcdef

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 RightPad

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

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

func RightPadBytes

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

RightPadBytes 从右填充到指定长度

func RunPath

func RunPath() string

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

func S2B

func S2B(s string) []byte

S2B StringToBytes converts string to byte slice without a memory allocation. Ref: gin

func SafeGo

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

SafeGo 带 Recover 的 goroutine 运行

func SliceBigFilter

func SliceBigFilter(a []int, f func(v int) bool) []int

SliceBigFilter 过滤切片元素 适合大切片

func SliceDeleteElem

func SliceDeleteElem(i int, s []any) ([]any, error)

SliceDeleteElem 索引删除 slice 元素

func SliceIntDeduplication

func SliceIntDeduplication(a []int) []int

SliceIntDeduplication 去除重复的元素

func SliceIntJoin

func SliceIntJoin(s []int, sep string) string

SliceIntJoin 整型数组拼接成字符串

func SliceReverse

func SliceReverse(s []any)

SliceReverse slice 反转

func SliceShuffle

func SliceShuffle(s []any)

SliceShuffle shuffle a slice

func SliceSmallFilter

func SliceSmallFilter(a []int, f func(v int) bool) []int

SliceSmallFilter 过滤切片元素 适合小切片

func SliceToInt

func SliceToInt(ss []string) (ii []int)

SliceToInt 字符串数组转换成整型数组

func SubInLocation

func SubInLocation(ts time.Time) float64

SubInLocation 计算时间差

func WaitSignal

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

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

Types

type RecoveryCallback

type RecoveryCallback func(err any, trace []byte)

RecoveryCallback 自定义恢复信息回调

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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