liveBook · Manning

Struct tag

As explained in Go in Action, Second Edition

A struct tag is metadata attached to a Go struct field that can guide reflection-based processing such as serialization and field mapping.

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

How Go in Action, Second Edition covers it

How deep it goes
Defines tag metadata and works through JSON and XML mappings, including options and XML-specific forms; leaves specialized validation to custom methods.
Examples
Uses a server configuration with JSON keys, omitted fields, XML attributes and elements, and conversion between formats.
What it assumes
Assumes familiarity with Go structs and exported fields while defining tag metadata before applying it to serialization.
What you can do after
Apply it via encoding/json and encoding/xml to map one exported-field struct to both formats in a configuration converter.
How it connects
Connects Go structs and exported fields to marshaling, unmarshaling, JSON, XML, and custom serialization methods.

Also explained in

Compare how 3 books explain struct tag →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Action, Second Edition. The section links point to the parts of the book it draws on.