Just started with Go and I have a small doubt from a tutorial I am following. I read that Unmarshall
is some kind of JSON encoding, my doubt here is: err = json.Unmarshal(body, &p)
why are we assigning the encoded body to err
and how is p.Stuff.Fruit
getting the value when I can't see anything assigned to p
.
Note : produce is different package which contains some type and arrays.*
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)
}