Why does json.Unmarshal work with reference but not pointer?

前端 未结 4 1989
不思量自难忘°
不思量自难忘° 2020-12-24 10:58

This example from the json.Unmarshal docs (slightly modified for simplicity to use Animal instead of []Animal) works, no errors:

Playground

4条回答
  •  有刺的猬
    2020-12-24 11:37

    Because your pointer is nil.

    If you initialize it it works: http://play.golang.org/p/zprmV0O1fG

    var animals *Animal = &Animal{}
    

    Also, it can be spelled either way (consistency in a single doc would be nice, though): http://en.wikipedia.org/wiki/Marshalling_(computer_science)

提交回复
热议问题