Struct Tags

structs are used to collect multiple pieces of information together in one unit. When you read this information from systems such as databases, or APIs, you can use struct tags to control how this information is assigned to the fields of a struct

Struct tag definition

A tag for a field allows you to attach meta-information to the field which can be acquired using reflection.

A struct tag looks like this, with the tag offset with backtick ` characters:

type User struct {
	Name string `json:"name" xml:"name"`
}

Other Go code is then capable of examining these structs and extracting the values assigned to specific keys it requests. Struct tags have no effect on the operation of your code without additional code that examines them.

Here is a list of commonly used tag keys: