I am struggling with deserializing an integer in a string structure field. The struct field is a string and is expected to be assigned from users of my library. Therefore, I want it to be a string, because to write it in the database, I really still do not care about the value inside. Users can provide text, but some simply assign integers.
Consider this structure:
type Test struct {
Foo string
}
Sometimes I get a JSON value that is valid but will not be deserialized into the structure because the Foo field is an integer instead of a string:
{ "foo": "1" }
{ "foo": 1 }
json.Unmarshal will explode with the following error:
json: cannot unmarshal number into Go struct field test.Foo of type string
Watch playback: https://play.golang.org/p/4Qau3umaVm
JSON ( ) , , , int . Go?
, , json.Unmarshal - Foo interface{}, ..
, ? json:",string"