-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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Yurkol
  • 1,414
  • 1
  • 19
  • 28
  • Possible duplicate of [My structures are not marshalling into json](http://stackoverflow.com/questions/15452004/my-structures-are-not-marshalling-into-json) – Yurkol 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,765
  • 6
  • 35
  • 56
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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189