I wrote an example program to illustrate my question, and it can be viewed here: https://play.golang.org/p/6776lYcbBR
So my question is:
when a structure (GameOne) field's name starts with a capital letter, json.Unmarshal works as expected; when it starts with a lower-case letter (GameTwo), the field value is set to its default.
Why is this so? Has it something to do with scope/visibility rules?
Thank you in advance.
json.Unmarshal
sets only the export fields in a struct and for exporting a field the first letter must be capital.
For more information I highly suggest you to take a look to the documentation
From the documentation (emphasis added):
Unmarshal will only set exported fields of the struct.
Fields which begin with a lowercase letter are, of course, not exported. So there's no way for the JSON marshaler (or indeed anything at all outside of your package) to affect them.