Tags in Golang

Michał Łowicki
Feb 19, 2018 · 5 min read

Declaration of struct fields can be enriched by string literal placed afterwards — tag. Tags add meta information used either by current package or external ones. Let’s first recall how struct declarations look like, then we’ll deep dive into tags themselves and we’ll wrap up with couple of use cases.

Struct type

Struct is a sequence of fields. Each field consists of optional name and required type (source code):

Field T1 is called an embedded field since it’s declared with type but no name.

Field declaration can specify more than one identifier like f3 and f4 from 3rd field declaration in T2.

Language specification states that each field declaration is followed by semicolon but as we’ve seen above it can be omitted. Semicolon might be useful if there is a need to put multiple fields declarations into the same line (source code):

Tag

A field declaration may be followed by an optional string literal (tag) which becomes an attribute of all the fields in the corresponding field declaration (single field declaration can specify multiple identifiers). Let’s see it in action (source code):

Either raw string literals or interpreted string literals can be used but conventional format described below requires raw string literals. Differences between raw and interpreted string literals are described in spec.

If field declarations contains more than one identifier then tag is attached to all fields from field declaration (like fields f4 and f5 above).

Reflection

Tags are accessible through reflect package which allows run-time reflection (source code):

Setting up empty tag has the same effect as not using tag at all (source code):

Conventional format

Introduced in commit “reflect: support for struct tag use by multiple packages” allows to set meta information per package. This provides simple namespacing. Tags are formatted as a concatenation of pairs. Key might be name of the package like json. Pairs can be optionally separated by spaces — . If conventional format is used then we can use two methods of struct tag (StructTag) — Get or Lookup. They allow to return value associated with desired key inside tag.

Lookup function returns two values — value associated with key (or blank if not set) and bool indicating if key has been found at all (source code):

Get method is simply wrapper of Lookup which discards boolean flag (source code):

Return value of Get or Lookup is unspecified if tag doesn’t have conventional format.

Even if tag is any string literal (interpreted or raw) then Lookup and Get methods will find value for key only if value is enclosed between double quotes (source code):

It’s possible to use escaped double quotes within interpreted strings literals (source code):

but it’s much less readable.

Conversion

Converting struct type value into other type requires that underlaying types are identical but tags are ignored (source code):

This behaviour has been introduced in Go 1.8 (proposal). In Go 1.7 and older above code could would throw a compile-time error.

Use cases

(Un)marshaling

Probably the most common use of tags in Go is marshalling. Let’s see how it’s used by function Marshal from json package (source code):

Package xml also takes advantage of tags — https://golang.org/pkg/encoding/xml/#MarshalIndent.

ORM

Object-relation mapping tools like GORM use tags extensively — example.

Digesting forms data

https://godoc.org/github.com/gorilla/schema

Other

There’re more potential uses cases of tags like configuration management, default values for structs, validation, command-line arguments description etc. (list of well-known struct tags).

go vet

Go compiler doesn’t enforce conventional format of struct tags but go vet does that so it’s worth to use it e.g. as a part of CI pipeline.

Thanks to struct tags programmers can benefit from single-sourcing. Go is a pragmatic language so even if it’s possible to solve JSON / XML encoding using other ways like dedicated data structures to control the whole process, Golang gives something which makes software engineer’s life a bit easier. It’s worth to mention that length of tag isn’t limited by spec.

👏👏👏 below to help others discover this story. Please follow me here or on Twitter if you want to get updates about new posts or boost work on future stories.

golangspec

A series dedicated to deeply understand Go’s specification…