V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
jxia
V2EX  ›  Go 编程语言

gookit/goutil - Go 一些常用的工具函数收集、整理实现

  •  1
     
  •   jxia · 2022-06-09 13:27:25 +08:00 · 1425 次点击
    这是一个创建于 689 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Go 常用的一些工具函数,数字,字符串,数组,Map ,文件,错误,时间日期,特殊处理,格式化,常用信息获取等等

    工具包

    • arrutil array/slice 相关操作的函数工具包. 如:类型转换,元素检查等等
    • dump 简单的变量打印工具,打印 slice, map 会自动换行显示每个元素,同时会显示打印调用位置
    • cliutil CLI 的一些工具函数包. eg: read input, exec command, cmdline parse/build
    • errorx 为 go 提供增强的错误实现,允许使用堆栈跟踪和包装另一个错误。
    • envutil ENV 信息获取判断工具包. eg: get one, get info, parse var
    • fsutil 文件系统操作相关的工具函数包. eg: file and dir check, operate
    • maputil map 相关操作的函数工具包. eg: convert, sub-value get, simple merge
    • mathutil, numutil int/number 相关操作的函数工具包. eg: convert, math calc, random
    • netutil/httpreq 包装 http.Client 实现的更加易于使用的 HTTP 客户端
    • strutil string 相关操作的函数工具包. eg: bytes, check, convert, encode, format and more
    • sysutil system 相关操作的函数工具包. eg: sysenv, exec, user, process
    • testutil test help 相关操作的函数工具包. eg: http test, mock ENV value
    • timex 提供增强的 time.Time 实现。添加更多常用的功能方法
      • 例如: DayStart(), DayAfter(), DayAgo(), DateFormat() 等等

    Git Repo:

    goutil/dump

    goutil/dump - 是一个 golang 数据打印工具包,可以打印出漂亮易读的 go slice, map, struct 数据。

    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

    3 条回复    2022-06-11 09:45:43 +08:00
    hailaz
        1
    hailaz  
       2022-06-10 10:30:32 +08:00
    https://github.com/gogf/gf
    有个差不多的
    jxia
        2
    jxia  
    OP
       2022-06-10 20:55:07 +08:00
    @hailaz gf 这是个开发框架啊
    349865361
        3
    349865361  
       2022-06-11 09:45:43 +08:00
    这个库刚开始搞 go 就发现在用了 已经用了 3 个月了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5338 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 07:04 · PVG 15:04 · LAX 00:04 · JFK 03:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.