-2

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.

Flimzy
  • 68,325
  • 15
  • 126
  • 165
RdB
  • 1,286
  • 1
  • 17
  • 27
  • Possible duplicate of [My structures are not marshalling into json](http://stackoverflow.com/questions/15452004/my-structures-are-not-marshalling-into-json) – RdB Feb 28 '17 at 14:54

2 Answers2

1

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

Tinwor
  • 7,255
  • 6
  • 31
  • 55
1

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.

Flimzy
  • 68,325
  • 15
  • 126
  • 165