Struct tags

March 17, 2023

Struct types

A field declaration may be followed by an optional string literal tag, which becomes an attribute for all the fields in the corresponding field declaration. An empty tag string is equivalent to an absent tag. The tags are made visible through a reflection interface and take part in type identity for structs but are otherwise ignored.

struct {
	x, y float64 ""  // an empty tag string is like an absent tag
	name string  "any string is permitted as a tag"
	_    [4]byte "ceci n'est pas un champ de structure"
}

// A struct corresponding to a TimeStamp protocol buffer.
// The tag strings define the protocol buffer field numbers;
// they follow the convention outlined by the reflect package.
struct {
	microsec  uint64 `protobuf:"1"`
	serverIP6 uint64 `protobuf:"2"`
}

Struct tags are many things. Confusing. Slightly magical. Powerful. Abused. Probably a few other things as well.

While any string is permitted as a tag (as shown in the example above), by convention they should conform to the format understood by reflect.StructTag, and any that don’t will cause go vet to complain.

You’ll most frequently see struct tags used to indicate a field’s name in an alternative representation, such as when expressed in JSON, YAML, or in a corresponding database table. You may even see them combined:

type Person struct {
	Name     string `json:"name" db:"name"`
	Age      int    `json:"age" db:"age"`
  Password string `json:"-" db:"pass"`
}

You may also interpret your own arbitrary struct tags by using the reflection library. And while you can use tags in any format, it’s worth stressing that you should stick to the format understood by reflect.StructTag, otherwise you’ll earn the ire of every other Go developer who has to use your (in their eyes) broken code!

Quotes from The Go Programming Language Specification Version of December 15, 2022


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .