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

前端 未结 4 1987
不思量自难忘°
不思量自难忘° 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:32

    I had a similiar condition before but in a different case. It is related with concept of interface in Go. If a function declares a interface as argument or return value, the caller have to pass or return the reference

    In your case, json.Unmarshal accept interface as second argument

提交回复
热议问题