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

polarisxu

共 3828字,需浏览 8分钟

 ·

2021-05-23 00:46

阅读本文大概需要 4 分钟。

大家好,我是 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。




往期推荐


我是 polarisxu,北大硕士毕业,曾在 360 等知名互联网公司工作,10多年技术研发与架构经验!2012 年接触 Go 语言并创建了 Go 语言中文网!著有《Go语言编程之旅》、开源图书《Go语言标准库》等。


坚持输出技术(包括 Go、Rust 等技术)、职场心得和创业感悟!欢迎关注「polarisxu」一起成长!也欢迎加我微信好友交流:gopherstudio


浏览 77
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报