← Back Home

Issue with white space in json struct tags

Go requires all exported fields to start with a capitalized letter. But It is not common for JSON where lower case letter keys are preferred. We can solve this problem by using the struct tags for json.

for an example:

According to Golang spec

But be careful when you create json tags. If there is any white space in json tag, struct will not be unMarshaled properly. If you have a struct such as:

When you unmarshal a json string to MyStruct type, it will give you zero value (empty string for a string). This is also very difficult to catch.