原文載于 https://old-panda.com/2019/12/11/golang-omitempty/
用法
熟悉 Golang 的朋友對(duì)于 json 和 struct 之間的轉(zhuǎn)換一定不陌生,為了將代碼中的結(jié)構(gòu)體與 json 數(shù)據(jù)解耦,通常我們會(huì)在結(jié)構(gòu)體的 field 類型后加上解釋說明,例如在表示一個(gè)地址的時(shí)候, json 數(shù)據(jù)如下所示
{
"street": "200 Larkin St",
"city": "San Francisco",
"state": "CA",
"zipcode": "94102"
}
與之相對(duì)應(yīng)的 Golang 結(jié)構(gòu)體表示可能是這個(gè)樣子的
type address struct {
Street string `json:"street"` // 街道
Ste string `json:"suite"` // 單元(可以不存在)
City string `json:"city"` // 城市
State string `json:"state"` // 州/省
Zipcode string `json:"zipcode"` // 郵編
}
這樣無論代碼中的變量如何改變,我們都能成功將 json 數(shù)據(jù)解析出來,獲得正確的街道,城市等信息,到目前為止一切正常。但如果我們想要將地址結(jié)構(gòu)體恢復(fù)成 json 格式時(shí),問題就來了。比方說我們用下面這段代碼讀取了地址 json ,然后根據(jù)業(yè)務(wù)邏輯處理了之后恢復(fù)成正常的 json 打印出來
func main() {
data := `{
"street": "200 Larkin St",
"city": "San Francisco",
"state": "CA",
"zipcode": "94102"
}`
addr := new(address)
json.Unmarshal([]byte(data), &addr)
// 處理了一番 addr 變量...
addressBytes, _ := json.MarshalIndent(addr, "", " ")
fmt.Printf("%s\n", string(addressBytes))
}
這段代碼的輸出是
{
"street": "200 Larkin St",
"suite": "",
"city": "San Francisco",
"state": "CA",
"zipcode": "94102"
}
多了一行 "suite": "", ,而這則信息在原本的 json 數(shù)據(jù)中是沒有的(在美國(guó)的地址中,如果不是群租公寓或者共享辦公樓, suite 這一條不存在很正常,人們直接用街道門牌號(hào)來表示地址就足夠了),但我們更希望的是,在一個(gè)地址有 suite 號(hào)碼的時(shí)候輸出,不存在 suite 的時(shí)候就不輸出,幸運(yùn)的是,我們可以在 Golang 的結(jié)構(gòu)體定義中添加 omitempty 關(guān)鍵字,來表示這條信息如果沒有提供,在序列化成 json 的時(shí)候就不要包含其默認(rèn)值。稍作修改,地址結(jié)構(gòu)體就變成了
type address struct {
Street string `json:"street"`
Ste string `json:"suite,omitempty"`
City string `json:"city"`
State string `json:"state"`
Zipcode string `json:"zipcode"`
}
重新運(yùn)行,即可得到正確的結(jié)果。
陷阱
帶來方便的同時(shí),使用 omitempty 也有些小陷阱,一個(gè)是該關(guān)鍵字無法忽略掉嵌套結(jié)構(gòu)體。還是拿地址類型說事,這回我們想要往地址結(jié)構(gòu)體中加一個(gè)新 field 來表示經(jīng)緯度,如果沒有缺乏相關(guān)的數(shù)據(jù),暫時(shí)可以忽略。新的 struct 定義如下所示
type address struct {
Street string `json:"street"`
Ste string `json:"suite,omitempty"`
City string `json:"city"`
State string `json:"state"`
Zipcode string `json:"zipcode"`
Coordinate coordinate `json:"coordinate,omitempty"`
}
type coordinate struct {
Lat float64 `json:"latitude"`
Lng float64 `json:"longitude"`
}
讀入原來的地址數(shù)據(jù),處理后序列化輸出,我們就會(huì)發(fā)現(xiàn)即使加上了 omitempty 關(guān)鍵字,輸出的 json 還是帶上了一個(gè)空的坐標(biāo)信息
{
"street": "200 Larkin St",
"city": "San Francisco",
"state": "CA",
"zipcode": "94102",
"coordinate": {
"latitude": 0,
"longitude": 0
}
}
為了達(dá)到我們想要的效果,可以把坐標(biāo)定義為指針類型,這樣 Golang 就能知道一個(gè)指針的“空值”是多少了,否則面對(duì)一個(gè)我們自定義的結(jié)構(gòu), Golang 是猜不出我們想要的空值的。于是有了如下的結(jié)構(gòu)體定義
type address struct {
Street string `json:"street"`
Ste string `json:"suite,omitempty"`
City string `json:"city"`
State string `json:"state"`
Zipcode string `json:"zipcode"`
Coordinate *coordinate `json:"coordinate,omitempty"`
}
type coordinate struct {
Lat float64 `json:"latitude"`
Lng float64 `json:"longitude"`
}
相應(yīng)的輸出為
{
"street": "200 Larkin St",
"city": "San Francisco",
"state": "CA",
"zipcode": "94102"
}
另一個(gè)“陷阱”是,對(duì)于用 omitempty 定義的 field ,如果給它賦的值恰好等于默認(rèn)空值的話,在轉(zhuǎn)為 json 之后也不會(huì)輸出這個(gè) field 。比如說上面定義的經(jīng)緯度坐標(biāo)結(jié)構(gòu)體,如果我們將經(jīng)緯度兩個(gè) field 都加上 omitempty
type coordinate struct {
Lat float64 `json:"latitude,omitempty"`
Lng float64 `json:"longitude,omitempty"`
}
然后我們對(duì)非洲幾內(nèi)亞灣的“原點(diǎn)坐標(biāo)”非常感興趣,于是編寫了如下代碼
func main() {
cData := `{
"latitude": 0.0,
"longitude": 0.0
}`
c := new(coordinate)
json.Unmarshal([]byte(cData), &c)
// 具體處理邏輯...
coordinateBytes, _ := json.MarshalIndent(c, "", " ")
fmt.Printf("%s\n", string(coordinateBytes))
}
最終我們得到了一個(gè)
{}
這個(gè)坐標(biāo)消失不見了!但我們的設(shè)想是,如果一個(gè)地點(diǎn)沒有經(jīng)緯度信息,則懸空,這沒有問題,但對(duì)于“原點(diǎn)坐標(biāo)”,我們?cè)诖_切知道它的經(jīng)緯度的情況下,(0.0, 0.0)仍然被忽略了。正確的寫法也是將結(jié)構(gòu)體內(nèi)的定義改為指針
type coordinate struct {
Lat *float64 `json:"latitude,omitempty"`
Lng *float64 `json:"longitude,omitempty"`
}
這樣空值就從 float64 的 0.0 變?yōu)榱酥羔橆愋偷?nil ,我們就能看到正確的經(jīng)緯度輸出。
{
"latitude": 0,
"longitude": 0
}
P.S. 本文中拿來作示例的地址是舊金山亞洲藝術(shù)博物館的地址,藏品豐富,上到夏商周,下至明清的文物都能看到,幾年前第一次去參觀,很是喜歡,印象深刻。