[Go 编程语言] gookit/goutil – Go 一些常用的工具函数收集、整理实现
Go 常用的一些工具函数,数字,字符串,数组,Map ,文件,错误,时间日期,特殊处理,格式化,常用信息获取等等
工具包
arrutil
array/slice 相关操作的函数工具包. 如:类型转换,元素检查等等dump
简单的变量打印工具,打印 slice, map 会自动换行显示每个元素,同时会显示打印调用位置cliutil
CLI 的一些工具函数包. eg: read input, exec command, cmdline parse/builderrorx
为 go 提供增强的错误实现,允许使用堆栈跟踪和包装另一个错误。envutil
ENV 信息获取判断工具包. eg: get one, get info, parse varfsutil
文件系统操作相关的工具函数包. eg: file and dir check, operatemaputil
map 相关操作的函数工具包. eg: convert, sub-value get, simple mergemathutil
,numutil
int/number 相关操作的函数工具包. eg: convert, math calc, randomnetutil/httpreq
包装 http.Client 实现的更加易于使用的 HTTP 客户端strutil
string 相关操作的函数工具包. eg: bytes, check, convert, encode, format and moresysutil
system 相关操作的函数工具包. eg: sysenv, exec, user, processtestutil
test help 相关操作的函数工具包. eg: http test, mock ENV value-
timex
提供增强的 time.Time 实现。添加更多常用的功能方法- 例如: DayStart(), DayAfter(), DayAgo(), DateFormat() 等等
Git Repo:
- Github: https://github.com/gookit/goutil
- Gitee: https://gitee.com/gookit/goutil
goutil/dump
goutil/dump – 是一个 golang 数据打印工具包,可以打印出漂亮易读的 go slice, map, struct 数据。
goutil/timex
goutil/timex 提供增强的 time.Time 实现。添加更多常用的功能方法。例如: DayStart(), DayAfter(), DayAgo(), DateFormat() 等等
创建 timex 实例
now := timex.Now() // from time.Time
tx := timex.New(time.Now())
tx := timex.FromTime(time.Now()) // from time unix
tx := timex.FromUnix(1647411580)
从日期时间字符串创建:
// auto match layout by datetime
tx, err := timex.FromString("2022-04-20 19:40:34")
// custom set the datetime layout
tx, err := timex.FromString("2022-04-20 19:40:34", "2006-01-02 15:04:05")
// use date template as layout
tx, err := timex.FromDate("2022-04-20 19:40:34", "Y-M-D H:I:S")
获取时间:
tx := timex.Now() tx.Yesterday()
tx.Tomorrow() tx.DayStart() // get time at Y-M-D 00:00:00
tx.DayEnd() // get time at Y-M-D 23:59:59
tx.HourStart() // get time at Y-M-D H:00:00
tx.HourEnd() // get time at Y-M-D H:59:59 tx.AddDay(2)
tx.AddHour(1)
tx.AddMinutes(15)
tx.AddSeconds(120)
更多
更多工具和使用请查看 README https://github.com/gookit/goutil/blob/master/README.zh-CN.md