Deserialization reconstructs a Go value from a stored or transmitted representation. It lets an application read structured input—such as a configuration file—into types it can inspect and validate. Go’s encoding packages provide format-specific tools, including encoding/json and encoding/xml. section 10.5 section 10.11
For JSON, define a struct with exported fields, then use `json` tags where JSON key names differ from Go field names. Call `json.Unmarshal` with the input bytes and a pointer to the destination value; check its returned error before using the result. Unmarshaling checks JSON values against Go field types and handles nested structures. section 10.5.1
Read a server configuration
Given JSON containing `"name"`, `"debug"`, and `"port"`, a matching destination can be defined and populated as follows: ```go type Config struct { Name string `json:"name"` Debug bool `json:"debug"` Port int `json:"port"` } var cfg Config if err := json.Unmarshal(data, &cfg); err != nil { return err } ``` Here, `data` is the configuration file’s JSON as a byte slice. A separate validation step can check whether the resulting values are sensible for the application. section 10.5.1
When decoding needs domain-specific behavior, a type can implement `UnmarshalJSON`. One pattern decodes into a temporary value, validates a field, and assigns the completed result only after validation succeeds. This keeps format parsing and application rules together without treating a successful type match as sufficient validation. section 10.5.1 section 10.11
Listing 10.17 Defining a configuration structure with JSON tags
// Config represents the structure of a JSON configuration file
type Config struct {
Name string `json:"name"` #1
Version string `json:"version"`
Debug bool `json:"debug"` #2
Port int `json:"port"` #3
Host string `json:"host,omitempty"` #4
LogLevel string `json:"-"` #5
}
Inspect the configuration struct’s exported fields and JSON tags to see how input keys map to a Go value. section 10.5.1









