For years when I saw json like:
{"timestamp": "2022-06-16T05:37:34.483Z"}
I would make my golang struct like:
type Foo struct {
Timestamp string `json:"timestamp"`
}
But then someone showed me that:
type Foo struct {
Timestamp time.Time `json:"timestamp"`
}
Will just magically parse it from that string into the time.Time thing I want!
My question is, where inside golang source is this done? What formats does it parse? Do other strings from json get "magically" parsed too? Could I define my own?
i.e. with json
{"foo": "my_own_format"}
type Foo struct {
Foo Bar `json:"foo"`
}
type Bar struct {
Words []string
}
Could I make the end result of this json.Unmarshal calling strings.Split("my_own_format", "_") and getting that Bar struct's Words to have "my", "own", "format" in its Words array?