gookit/goutil - 发布 v0.6.10 版本, Go 常用功能的扩展工具库

313 天前
 jxia

gookit/goutil Go 常用功能的扩展工具库。包含:数字,字符串,slice/数组,Map ,结构体,反射,文本,文件,错误,时间日期,测试,CLI ,命令运行,系统信息,格式化,常用信息获取等等。

Github: https://github.com/gookit/goutil

v0.6.10 更新记录

完整变更日志 v0.6.9...v0.6.10

✨ 新功能

👔 更新调整

其他调整

部分新增功能使用

dump 打印自定义类型

dump 特殊处理自定义的 int 、uint 类型值,将会打印 String 格式的说明

初始化结构体

type ExtraDefault struct {
    City   string `default:"some where"`
    Github string `default:"${ GITHUB_ADDR }"`
}

type User struct {
    Name  string        `default:"inhere"`
    Age   int           `default:"300"`
    Extra *ExtraDefault `default:""` // 标记需要初始化
}

optFn := func(opt *structs.InitOptions) {
    opt.ParseEnv = true
}

obj := &User{}
err := structs.InitDefaults(obj, optFn)
goutil.PanicErr(err)

dump.P(obj)

初始化结果:

&structs_test.User {
  Name: string("inhere"), #len=6
  Age: int(300),
  Extra: &structs_test.ExtraDefault {
    City: string("some where"), #len=10
    Github: string("https://some .... url"), #len=21
  },
},

使用 echo server 测试

使用 testutil.NewEchoServer() 可以快速的创建一个 HTTP echo server. 方便测试 HTTP 请求,响应等。

使用示例


var testSrvAddr string

func TestMain(m *testing.M) {
    s := testutil.NewEchoServer()
    defer s.Close()

    testSrvAddr = "http://" + s.Listener.Addr().String()
    fmt.Println("server addr:", testSrvAddr)

    m.Run()
}

func TestNewEchoServer(t *testing.T) {
    // 可直接请求测试 server
    r, err := http.Post(testSrvAddr, "text/plain", strings.NewReader("hello!"))
    assert.NoErr(t, err)

    // 将响应信息绑定到 testutil.EchoReply
    rr := testutil.ParseRespToReply(r)
    dump.P(rr)
    assert.Eq(t, "POST", rr.Method)
    assert.Eq(t, "text/plain", rr.ContentType())
    assert.Eq(t, "hello!", rr.Body)
}

v0.6.9 更新记录

完整变更日志 v0.6.8...v0.6.9

✨ 新功能

👔 更新调整

其他调整

部分功能使用示例

finder 文件查找使用

fsutil/finder 提供了简单快速的方式查找匹配文件、目录。

ff := finder.NewFinder("/path/to/dir/").
    // OnlyFindDir(). // 默认只只查找文件
    UseAbsPath().
    WithoutDotDir().
    WithDirName("testdata")

// Find() 返回 chan, 可以 for 处理查找结果
for el := range f.Find() {
    fmt.Println(el.Path())
}

strutil.ParseSizeRange

可以简单方便的将字符串大小范围解析为 byte size

opt := &strutil.ParseSizeOpt{}

mix, max, err := strutil.ParseSizeRange("1kb~1mb", opt)
goutil.PanicErr(err)
fmt.Println(min, max) // OUTPUT: 1024, 1048576

支持的表达式格式示例:

"1KB~2MB"       => 1KB to 2MB
"-1KB"          => <1KB
"~1MB"          => <1MB
"< 1KB"         => <1KB
"1KB"           => >1KB
"1KB~"          => >1KB
">1KB"          => >1KB
"+1KB"          => >1KB

timex.ParseRange()

timex.ParseRange() 可以简单快速的将相对的时间大小范围、或关键字解析为 time.Time

start, end, err := ParseRange("-1h~1h", nil)
goutil.PanicErr(err)


fmt.Println(start, end)

支持的表达式格式示例:

"-5h~-1h"           => 5 hours ago to 1 hour ago
"1h~5h"             => 1 hour after to 5 hours after
"-1h~1h"            => 1 hour ago to 1 hour after
"-1h"               => 1 hour ago to feature. eq "-1h~"
"-1h~0"             => 1 hour ago to now.
"< -1h" OR "~-1h"   => 1 hour ago.
"> 1h" OR "1h"      => 1 hour after to feature

// keyword: now, today, yesterday, tomorrow
"today"          => today start to today end
"yesterday"      => yesterday start to yesterday end
"tomorrow"       => tomorrow start to tomorrow end

更多信息

更多使用说明请看 README 以及相关方法的单元测试

Github: https://github.com/gookit/goutil

870 次点击
所在节点    Go 编程语言
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/953921

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX