This question already has an answer here:
- json.Marshal(struct) returns “{}” 2 answers
- JSON and dealing with unexported fields 2 answers
- Printing Empty Json as a result [duplicate] 1 answer
I was trying to follow this tutorial:
https://gowebexamples.com/json/
and personalised it a bit, so instead of having the User struct like:
type User struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
Age int `json:"age"`
}
I had it like:
type User struct {
firstName string `json:"first_name"`
lastName string `json:"last_name"`
age int `json:"age"`
}
doing the call, with the correct json fields I would get empty strings in the name and 0 for the age...
I copied the code and pasted it so that I was sure the problem was my code(it was). Turns out the problem was because my User fields were not capitalised... if I have u.firstname
it does not work, but if I have u.Firstname
it works...
I fixed my code but now I cant understand why is it that the name of the fields in the struct not being capitalised are the problem, can someone please explain that to my dumb ass brain?
Thanks.
</div>