【发布时间】:2018-08-05 17:45:06
【问题描述】:
刚开始使用 Go,我对正在学习的教程有一点疑问。我读到Unmarshall 是某种 JSON 编码,我的疑问是:err = json.Unmarshal(body, &p) 为什么我们将编码主体分配给 err 以及当我看不到分配给的任何内容时,p.Stuff.Fruit 如何获得值p.
注意:produce 是不同的包,其中包含一些类型和数组。*
func main() {
url := "http://localhost:12337"
res, err := http.Get(url)
if err != nil {
panic(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}
var p produce.Payload
err = json.Unmarshal(body, &p) // I cant get this
if err != nil {
panic(err)
}
// how are these getting the values assigned to them
fmt.Println(p.Stuff.Fruit)
fmt.Println(p.Stuff.Veggies)
}
【问题讨论】:
-
经验法则:阅读软件包文档。总是,即使遵循一些随机教程。
标签: json pointers go unmarshalling