推薦一位高產(chǎn)的 Go 開(kāi)源庫(kù)作者
閱讀本文大概需要 4 分鐘。
大家好,我是 polarisxu。
今天給大家推薦一位高產(chǎn)的 Go 開(kāi)源庫(kù)作者,他就是 Josh Baker,GitHub:https://github.com/tidwall,有些人應(yīng)該不陌生吧?沒(méi)見(jiàn)過(guò),那這個(gè)庫(kù)見(jiàn)過(guò)嗎?gjson。如果還沒(méi)見(jiàn)過(guò),我只能說(shuō)你弱爆了~
我給大家介紹下他的幾個(gè)核心開(kāi)源庫(kù)。
01 gjson
這是快速的 JSON 解析庫(kù)。如果你只是想從 JSON 中獲取值,特別是復(fù)雜 JSON,定義結(jié)構(gòu)體解析麻煩,gjson 絕對(duì)值得一試。
比如這樣的一個(gè) 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"]}
]
}
現(xiàn)在有這樣的需求:
獲取 name 的 last 值? 獲取 children 的數(shù)量? 獲取第二個(gè) children? 獲取所有 friends 的 first 值? 獲取第二個(gè) friends 的 last 值? 獲取 friends 中 last 為 Murphy 的 first 值? 。。。
面對(duì)上面的需求,你怎么處理?
gjson 通過(guò) Path 語(yǔ)法可以快速解決上面的需求。
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 的數(shù)量?
fmt.Println("children.# =", result.Get("children.#").Int())
// 獲取第二個(gè) children?
fmt.Println("children.1 =", result.Get("children.1").String())
// 獲取所有 friends 的 first 值?
fmt.Println("friends.#.first =", result.Get("friends.#.first").Array())
// 獲取第二個(gè) 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())
}
是不是超級(jí)方便?!甚至可以方便找到 friends 中 age 大于多少的人。這里可以直接運(yùn)行測(cè)試:https://play.studygolang.com/p/lXFIfNJjuZ5。
項(xiàng)目地址:https://github.com/tidwall/gjson。
02 sjson
從名字可以看出來(lái),gjson 是 get json,而 sjson 就是 set json 了。不過(guò)這個(gè)庫(kù)的使用場(chǎng)景會(huì)比較少些。
項(xiàng)目地址:https://github.com/tidwall/sjson。
03 tile38
這是一個(gè)實(shí)時(shí)的地理空間和地理位置庫(kù)。作者還專(zhuān)門(mén)創(chuàng)建了一個(gè)網(wǎng)站:https://tile38.com/。
04 evio
這是一個(gè)時(shí)間循環(huán)網(wǎng)絡(luò)框架。有別于 Go 中常規(guī)的網(wǎng)絡(luò)框架,這個(gè)框架基于事件。它直接使用 epoll 和 kqueue 系統(tǒng)調(diào)用而不是使用標(biāo)準(zhǔn)的 go net 包。因此,這個(gè)庫(kù)和 C 語(yǔ)言的 libuv 和 libevent 類(lèi)似。
項(xiàng)目地址:https://github.com/tidwall/evio。
05 buntdb
buntdb 是一個(gè)嵌入式內(nèi)存的 Key/Value 數(shù)據(jù)庫(kù),用于定制索引和地理空間支持。
項(xiàng)目地址:https://github.com/tidwall/buntdb。
06 小結(jié)
除了以上列舉的庫(kù),還有很多其他庫(kù)。目前看一共有 130+ 個(gè),其中 9 個(gè) star 數(shù)超過(guò) 1k。該作者所有項(xiàng)目:https://github.com/tidwall?tab=repositories&q=&type=source&language=&sort=stargazers。
大家在 GitHub 上可以多 follow 一些高產(chǎn)的作者,發(fā)現(xiàn)好項(xiàng)目。
推廣下我自己:歡迎在 GitHub 上 follow 我,我每周都會(huì) star 不少 Go 的好項(xiàng)目,follow 我可以看到我的動(dòng)態(tài)。閱讀原文就可以直達(dá)我的 GitHub 主頁(yè):https://github.com/polaris1119。
我是 polarisxu,北大碩士畢業(yè),曾在 360 等知名互聯(lián)網(wǎng)公司工作,10多年技術(shù)研發(fā)與架構(gòu)經(jīng)驗(yàn)!2012 年接觸 Go 語(yǔ)言并創(chuàng)建了 Go 語(yǔ)言中文網(wǎng)!著有《Go語(yǔ)言編程之旅》、開(kāi)源圖書(shū)《Go語(yǔ)言標(biāo)準(zhǔn)庫(kù)》等。
堅(jiān)持輸出技術(shù)(包括 Go、Rust 等技術(shù))、職場(chǎng)心得和創(chuàng)業(yè)感悟!歡迎關(guān)注「polarisxu」一起成長(zhǎng)!也歡迎加我微信好友交流:gopherstudio
