推荐一位高产的 Go 开源库作者

polaris · 2021-05-19 10:14:32 · 2007 次点击 · 大约8小时之前 开始浏览    置顶
这是一个创建于 2021-05-19 10:14:32 的主题,其中的信息可能已经有所发展或是发生改变。

大家好,我是 polarisxu。

今天给大家推荐一位高产的 Go 开源库作者,他就是 Josh Baker,GitHub:https://github.com/tidwall,有些人应该不陌生吧?没见过,那这个库见过吗?gjson。如果还没见过,我只能说你弱爆了~

我给大家介绍下他的几个核心开源库。

01 gjson

这是快速的 JSON 解析库。如果你只是想从 JSON 中获取值,特别是复杂 JSON,定义结构体解析麻烦,gjson 绝对值得一试。

比如这样的一个 JSON 字符串:

{
  "name": {"first": "Tom", "last": "Anderson"},
  "age":37,
  "children": ["Sara","Alex","Jack"],
  "fav.movie": "Deer Hunter",
  "friends": [
    {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]},
    {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]},
    {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]}
  ]
}

现在有这样的需求:

  • 获取 name 的 last 值?
  • 获取 children 的数量?
  • 获取第二个 children?
  • 获取所有 friends 的 first 值?
  • 获取第二个 friends 的 last 值?
  • 获取 friends 中 last 为 Murphy 的 first 值?
  • 。。。

面对上面的需求,你怎么处理?

gjson 通过 Path 语法可以快速解决上面的需求。

const str = `{
  "name": {"first": "Tom", "last": "Anderson"},
  "age":37,
  "children": ["Sara","Alex","Jack"],
  "fav.movie": "Deer Hunter",
  "friends": [
    {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]},
    {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]},
    {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]}
  ]
}`

func main() {
    result := gjson.Parse(str)

    // 获取 name 的 last 值?
    fmt.Println("name.last =", result.Get("name.last").String())

    // 获取 children 的数量?
    fmt.Println("children.# =", result.Get("children.#").Int())

    // 获取第二个 children?
    fmt.Println("children.1 =", result.Get("children.1").String())

    // 获取所有 friends 的 first 值?
    fmt.Println("friends.#.first =", result.Get("friends.#.first").Array())

    // 获取第二个 friends 的 last 值?
    fmt.Println("friends.1.last =", result.Get("friends.1.last").String())

    // 获取 friends 中 last 为 Murphy 的 first 值?
    fmt.Println(`friends.#(last=="Murphy").first =`, result.Get(`friends.#(last=="Murphy").first`).String())
}

是不是超级方便?!甚至可以方便找到 friends 中 age 大于多少的人。这里可以直接运行测试:https://play.studygolang.com/p/lXFIfNJjuZ5

项目地址:https://github.com/tidwall/gjson

02 sjson

从名字可以看出来,gjson 是 get json,而 sjson 就是 set json 了。不过这个库的使用场景会比较少些。

项目地址:https://github.com/tidwall/sjson

03 tile38

这是一个实时的地理空间和地理位置库。作者还专门创建了一个网站:https://tile38.com/

04 evio

这是一个时间循环网络框架。有别于 Go 中常规的网络框架,这个框架基于事件。它直接使用 epoll 和 kqueue 系统调用而不是使用标准的 go net 包。因此,这个库和 C 语言的 libuv 和 libevent 类似。

项目地址:https://github.com/tidwall/evio

05 buntdb

buntdb 是一个嵌入式内存的 Key/Value 数据库,用于定制索引和地理空间支持。

项目地址:https://github.com/tidwall/buntdb

06 小结

除了以上列举的库,还有很多其他库。目前看一共有 130+ 个,其中 9 个 star 数超过 1k。该作者所有项目:https://github.com/tidwall?tab=repositories&q=&type=source&language=&sort=stargazers

大家在 GitHub 上可以多 follow 一些高产的作者,发现好项目。

推广下我自己:欢迎在 GitHub 上 follow 我,我每周都会 star 不少 Go 的好项目,follow 我可以看到我的动态。阅读原文就可以直达我的 GitHub 主页:https://github.com/polaris1119


有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2007 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传