So I have this struct in Go:
type Car struct {
Name string `json:"name"`
Speed int `json:"speed"`
}
And I have two JSON samples that I unmarshal:
str := `{"name": "", "speed": 0}`
strTwo := `{}`
I do the unmarshaling in this way:
car := Car{}
_ = json.Unmarshal([]byte(str), &car)
carTwo := Car{}
_ = json.Unmarshal([]byte(strTwo), &carTwo)
Now because of the way Go deals with default value types, when I try to print the structure, I get the same result:
car - { 0}
carTwo - { 0}
So I can't see the difference between a missing value in JSON and when a default value is passed. How can I solve this problem?
One way is to use pointers in struct:
type Car struct {
Name *string `json:"name"`
Speed *int `json:"speed"`
}
But I get a very ugly code when using this values, I have to do pointer dereferencing everywhere