Unmarshalling is the process of converting JSON data into Go struct instances. The json.Unmarshal function takes a byte slice of JSON data and a pointer to a struct, and populates the struct with values from the JSON. This is essential for working with APIs and parsing JSON from files or network requests.
Struct tags tell the JSON decoder which JSON field maps to which struct field. The json:"fieldname" tag is the standard way to specify this mapping. If a tag is omitted, the decoder uses the struct field name (case-insensitive matching). You can also use options like omitempty and string to control marshalling behavior.
When unmarshalling, the decoder is forgiving with types to a degree. For example, numeric JSON values will be converted to the appropriate Go numeric type if possible. If the JSON structure doesn’t match your struct exactly, unmarshal simply ignores extra fields and leaves missing fields with their zero values.