A struct tag is a specially formatted string attached to a Go struct field. Serialization packages read that metadata to decide how a field maps to external data. For a server configuration, `encoding/json` uses `json` tags to map exported Go fields such as `Name` and `Port` to JSON keys such as `name` and `port`. Fields must be exported for the package to marshal or unmarshal them. section 10.5.1
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
}
The configuration struct shows JSON key names, an `omitempty` option, and a field excluded with `json:"-"`. section 10.5.1
The same struct can carry tags for more than one format. `encoding/xml` reads `xml` tags to map fields to elements or attributes; its tag syntax also supports XML features such as comments and namespaces. Keeping `json` and `xml` tags on one configuration type lets a converter unmarshal input into a Go value, then marshal that value in the requested format. section 10.5.2
.Defining a configuration structure with XML tags
[source,go,linenums]
----
type Config struct {
XMLName xml.Name `xml:"configuration"` // Root element name
Name string `json:"name" xml:"name,attr"` // XML attribute
Version string `json:"version" xml:"version,attr"` // XML attribute
Namespace string `xml:"xmlns,attr,omitempty"` // XML namespace
Debug bool `json:"debug" xml:"debug"`
Port int `json:"port" xml:"port"`
Host string `json:"host" xml:"host,omitempty"`
Comment string `xml:",comment"` // XML comment
}
----
The dual-tagged configuration shows JSON keys alongside XML attributes, elements, a root name, a namespace, and a comment field. section 10.5.2
Tags control field mapping, not every validation or output rule. Unmarshaling checks whether incoming JSON types fit the target Go types. When a configuration needs additional behavior—such as validating a host URL or redacting a value during serialization—custom `UnmarshalJSON` or `MarshalJSON` methods provide that control. section 10.5.1 section 10.11


