I have struct
of Request, value is optional:
type Request struct {
Operation string `json:"operation"`
Key string `json:"key"`
Value string `json:"value"`
}
And function that should parse json string to struct^
go func() {
s := string("{'operation': 'get', 'key': 'example'}")
data := Request{}
json.Unmarshal([]byte(s), data)
log.Printf("Operation: %s", data.Operation)
}
For some reason data.Operation is empty. What is wrong here?
Two problems, first, your json is invalid, it needs to use
"
instead of'
Second, you have to unmarshal into
&data
and not todata
https://play.golang.org/p/zdMq5_ex8G
Side note, you would have seen this, if you would have been checking your errors: