第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 json.decoder 省略空的 json 字段

如何使用 json.decoder 省略空的 json 字段

Go
BIG陽 2022-04-25 17:24:07
我試圖理解為什么兩個函數(shù)返回相同的輸出。據(jù)我了解,省略 empty 的目的是不將該鍵添加到結(jié)果結(jié)構(gòu)中。我寫了這個例子,我希望第一個輸出沒有“空”鍵,但由于某種原因,它的值仍然顯示為 0。package mainimport (    "encoding/json"    "fmt"    "strings")type agentOmitEmpty struct {    Alias   string `json:"Alias,omitempty"`    Skilled bool   `json:"Skilled,omitempty"`    FinID   int32  `json:"FinId,omitempty"`    Empty   int    `json:"Empty,omitempty"`}type agent struct {    Alias   string `json:"Alias"`    Skilled bool   `json:"Skilled"`    FinID   int32  `json:"FinId"`    Empty   int    `json:"Empty"`}func main() {    jsonString := `{        "Alias":"Robert",        "Skilled":true,        "FinId":12345    }`    fmt.Printf("output with omit emtpy: %v\n", withEmpty(strings.NewReader(jsonString)))    // output with omit emtpy: {Robert true 12345 0}    fmt.Printf("output regular: %v\n", withoutEmpty(strings.NewReader(jsonString)))    // output without omit: {Robert true 12345 0}}func withEmpty(r *strings.Reader) agentOmitEmpty {    dec := json.NewDecoder(r)    body := agentOmitEmpty{}    err := dec.Decode(&body)    if err != nil {        panic(err)    }    return body}func withoutEmpty(r *strings.Reader) agent {    dec := json.NewDecoder(r)    body := agent{}    err := dec.Decode(&body)    if err != nil {        panic(err)    }    return body}
查看完整描述

1 回答

?
慕碼人8056858

TA貢獻(xiàn)1803條經(jīng)驗 獲得超6個贊

您需要定義 Empty ,*int以便在沒有值時將其替換為 nil 。然后它不會保存在數(shù)據(jù)庫中。



查看完整回答
反對 回復(fù) 2022-04-25
  • 1 回答
  • 0 關(guān)注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號