I have the following code:
package main
import "encoding/json"
import "fmt"
type SuperNum struct {
num string
}
func main() {
byt := []byte(`{"num":"6.13"}`)
var dat SuperNum
if err := json.Unmarshal(byt, &dat); err != nil {
panic(err)
}
fmt.Printf("%+v", dat) // I expect this to have a `num` attribute
}
Output:
{num:}
Program exited.
You can run this code in the golang playground.
Because I'm setting a num
property in the struct and in the JSON and they're both strings, I would have expected the dat
struct to have a num
property, with 'hello'
, but it doesn't.
What am I doing wrong? What in my mental model of how this should work is incorrect?
EDIT
I tried adding the json
signature to the struct, but it makes no difference (no idea what that actually does).
type SuperNum struct {
num string `json:"num"`
}