- A struct tag is a string literal (because it has the string type)
- Key is an unquoted string literal
- Value is a quoted string literal
- Key and value are separated by a colon (:). A key together with a value, separated by the colon is called a “key value pair“
- A struct tag can contain multiple key value pairs (optional). The key-value pairs are separated by a space.
- Not part of the definition is the options setting. Packages like encoding/json read the value as a comma-separated list. Anything after the first comma is part of the options, i.e: “foo,omitempty,string“. Here the value has a name called “foo” and options [“omitempty”, “string”]
- Because the struct tag is a string literal, it needs to be quoted either with a double quote or backtick. Because the value has to be quoted as well we always use backticks.
The ultimate guide to writing a Go tool
Tags in Golang
Go Tags explained
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.