我有一個(gè)這樣的結(jié)構(gòu):package mainimport ( "encoding/json" "fmt")type request struct { Version string `json:"version"` Operations map[string]operation `json:"operations"`}type operation struct { Type string `json:"type"` Width int `json:"width"` Height int `json:"height"`}func main() { jsonStr := "{\"version\": \"1.0\", \"operations\": {\"0\": {\"type\": \"type1\", \"width\": 100}, \"1\": {\"type\": \"type2\", \"height\": 200}}}" req := request{ Version: "1.0", } err := json.Unmarshal([]byte(jsonStr), &req) if err != nil { fmt.Println(err.Error()) } else { fmt.Println(req) }}我可以將 Version = "1.0" 設(shè)置為默認(rèn)值,但如何將默認(rèn)值設(shè)置為 Width 和 Height?
在golang中執(zhí)行json Unmarshal時(shí)如何將默認(rèn)值設(shè)置為映射值?
炎炎設(shè)計(jì)
2021-12-20 10:52:24