Documentation ¶
Overview ¶
Package utils is a library of commonly used utility functions.
Index ¶
- Constants
- func AdaptiveMongodbDsn(dsn string) string
- func AdaptiveMysqlDsn(dsn string) string
- func AdaptivePostgresqlDsn(dsn string) string
- func AdaptiveSqlite(dbFile string) string
- func DeleteBrackets(str string) string
- func FormatDateTimeLayout(t time.Time) string
- func FormatDateTimeLayoutWithMS(t time.Time) string
- func FormatDateTimeLayoutWithMSAndTZ(t time.Time) string
- func FormatDateTimeRFC3339(t time.Time) string
- func GetAvailablePort() (int, error)
- func GetHostname() string
- func GetLocalHTTPAddrPairs() (serverAddr string, requestAddr string)
- func Int64ToStr(v int64) string
- func IntToProtoInt32(v int) int32
- func IntToStr(v int) string
- func ParseDateTimeLayout(s string) (time.Time, error)
- func ParseDateTimeLayoutWithMS(s string) (time.Time, error)
- func ParseDateTimeLayoutWithMSAndTZ(s string) (time.Time, error)
- func ParseDateTimeRFC3339(s string) (time.Time, error)
- func ProtoInt32ToInt(v int32) int
- func ProtoInt64ToUint64(v int64) uint64
- func SafeRun(ctx context.Context, fn func(ctx context.Context))
- func SafeRunWithTimeout(d time.Duration, fn func(cancel context.CancelFunc))
- func StrToFloat32(str string) float32
- func StrToFloat32E(str string) (float32, error)
- func StrToFloat64(str string) float64
- func StrToFloat64E(str string) (float64, error)
- func StrToInt(str string) int
- func StrToIntE(str string) (int, error)
- func StrToUint32(str string) uint32
- func StrToUint32E(str string) (uint32, error)
- func StrToUint64(str string) uint64
- func StrToUint64E(str string) (uint64, error)
- func Uint64ToProtoInt64(v uint64) int64
- func Uint64ToStr(v uint64) string
- type WaitPrinter
Constants ¶
const ( // DateTimeLayout is the layout string for datetime format. DateTimeLayout = "2006-01-02 15:04:05" // DateTimeLayoutWithMS is the layout string for datetime format with milliseconds. DateTimeLayoutWithMS = "2006-01-02 15:04:05.000" // RFC3339 is the layout string for RFC3339 format. RFC3339 = "2006-01-02T15:04:05Z07:00" // DateTimeLayoutWithMSAndTZ is the layout string for datetime format with milliseconds and timezone. DateTimeLayoutWithMSAndTZ = "2006-01-02T15:04:05.000Z" // TimeLayout is the layout string for time format. TimeLayout = "15:04:05" // DateLayout is the layout string for date format. DateLayout = "2006-01-02" )
const MaxStringID = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
MaxStringID is the maximum string ID
Variables ¶
This section is empty.
Functions ¶
func AdaptiveMongodbDsn ¶
AdaptiveMongodbDsn 适应 MongoDB 的 DSN 如果 DSN 不以 "mongodb://" 或 "mongodb+srv://" 开头,则添加默认前缀 "mongodb://" 删除 DSN 中的括号
func AdaptiveMysqlDsn ¶
AdaptiveMysqlDsn 适应各种 MySQL 格式的 DSN 地址 将 "mysql://" 前缀替换为空字符串
func AdaptivePostgresqlDsn ¶
AdaptivePostgresqlDsn 将 PostgreSQL 的 DSN 转换为键值对字符串 如果 DSN 中包含空格超过 3 个,则直接返回原字符串 如果 DSN 不以 "postgres://" 开头,则添加前缀 删除 DSN 中的括号 解析 DSN 并设置默认的 sslmode 为 disable 返回格式化的键值对字符串
func AdaptiveSqlite ¶
AdaptiveSqlite 适应 SQLite 数据库文件路径 TODO: 转换为绝对路径
func DeleteBrackets ¶ added in v1.1.22
DeleteBrackets 删除 DSN 中的括号 查找并删除形如 "@(host:port)/" 的括号
func FormatDateTimeLayout ¶
FormatDateTimeLayout formats the given time to the layout string "2006-01-02 15:04:05".
func FormatDateTimeLayoutWithMS ¶
FormatDateTimeLayoutWithMS formats the given time to the layout string "2006-01-02 15:04:05.000".
func FormatDateTimeLayoutWithMSAndTZ ¶
FormatDateTimeLayoutWithMSAndTZ formats the given time to the layout string "2006-01-02T15:04:05.000Z".
func FormatDateTimeRFC3339 ¶
FormatDateTimeRFC3339 formats the given time to the layout string "2006-01-02T15:04:05Z07:00".
func GetLocalHTTPAddrPairs ¶
GetLocalHTTPAddrPairs get available http server and request address
func IntToProtoInt32 ¶ added in v1.1.22
IntToProtoInt32 convert int to proto int32
func ParseDateTimeLayout ¶
ParseDateTimeLayout parses the given string to time with layout string "2006-01-02 15:04:05".
func ParseDateTimeLayoutWithMS ¶
ParseDateTimeLayoutWithMS parses the given string to time with layout string "2006-01-02 15:04:05.000".
func ParseDateTimeLayoutWithMSAndTZ ¶
ParseDateTimeLayoutWithMSAndTZ parses the given string to time with layout string "2006-01-02T15:04:05.000Z".
func ParseDateTimeRFC3339 ¶
ParseDateTimeRFC3339 parses the given string to time with layout string "2006-01-02T15:04:05Z07:00".
func ProtoInt32ToInt ¶ added in v1.1.22
ProtoInt32ToInt convert proto int32 to int
func ProtoInt64ToUint64 ¶ added in v1.1.22
ProtoInt64ToUint64 convert proto int64 to uint64
func SafeRunWithTimeout ¶
func SafeRunWithTimeout(d time.Duration, fn func(cancel context.CancelFunc))
SafeRunWithTimeout safe run with limit timeouts
func StrToFloat32E ¶
StrToFloat32E string to float32 with error
func StrToFloat64E ¶
StrToFloat64E string to float64 with error
func StrToUint32E ¶
StrToUint32E string to uint32 with error
func StrToUint64E ¶
StrToUint64E string to uint64 with error
func Uint64ToProtoInt64 ¶ added in v1.1.22
Uint64ToProtoInt64 convert uint64 to proto int64
Types ¶
type WaitPrinter ¶ added in v1.1.22
type WaitPrinter struct {
// contains filtered or unexported fields
}
WaitPrinter 是一个等待打印器,用于在长时间操作期间显示等待提示信息。
func NewWaitPrinter ¶ added in v1.1.22
func NewWaitPrinter(interval time.Duration) *WaitPrinter
NewWaitPrinter 创建一个新的 WaitPrinter 实例。
func (*WaitPrinter) LoopPrint ¶ added in v1.1.22
func (p *WaitPrinter) LoopPrint(runningTip string)
LoopPrint 启动等待循环并打印运行提示信息。
func (*WaitPrinter) StopPrint ¶ added in v1.1.22
func (p *WaitPrinter) StopPrint(tip string)
StopPrint 停止等待循环并打印提示信息。