https://go.dev/play/p/PbCqmPcxKq1
I have no idea what I'm going wrong, I would expect some kind of error it it failed to unmarshal, but I just get nothing.
#json.Unmarshal giving default values and no error
9 messages · Page 1 of 1 (latest)
The struct fields are not exported so they can't be seen by other packages.
Fields must be exported for this to work.
Fields must start with an upper case character for them to exported.
You need to do it like this:
type ManifestLayer struct {
MediaType string `json:"mediaType"`
Size int64 `json:"size"`
Digest string `json:"digest"`
}
type OciManifest struct {
SchemaVersion int64 `json:"schemaVersion"`
MediaType string `json:"mediaType"`
Config ManifestLayer `json:"config"`
Layers []ManifestLayer `json:"layers"`
}
Ahhh, cool, thanks!
You're welcome. 🙂
Those things that I added to the struct are called "struct tags". The encoding/json package look them up at runtime. If they don't exist, it'll use the field's name.
So by adding the struct tags, the fields can be exported but the JSON keys will start with lower case.
If your question has been solved, please close the thread with </solved:1022173889255706726>