go-structtag

encoding/json unmarshal missing a field

醉酒当歌 提交于 2021-01-26 19:15:11
问题 The following code unmarshal's the "Id", but not the "Hostname". Why? I've been staring at it for long enough now that if it's a typo I know I'll never spot it. Help please. (http://play.golang.org/p/DIRa2MvvAV) package main import ( "encoding/json" "fmt" ) type jsonStatus struct { Hostname string `json:host` Id string `json:id` } func main() { msg := []byte(`{"host":"Host","id":"Identifier"}`) status := new(jsonStatus) err := json.Unmarshal(msg, &status) if err != nil { fmt.Println(

encoding/json unmarshal missing a field

ぃ、小莉子 提交于 2021-01-26 19:15:08
问题 The following code unmarshal's the "Id", but not the "Hostname". Why? I've been staring at it for long enough now that if it's a typo I know I'll never spot it. Help please. (http://play.golang.org/p/DIRa2MvvAV) package main import ( "encoding/json" "fmt" ) type jsonStatus struct { Hostname string `json:host` Id string `json:id` } func main() { msg := []byte(`{"host":"Host","id":"Identifier"}`) status := new(jsonStatus) err := json.Unmarshal(msg, &status) if err != nil { fmt.Println(