Go to label description

How tags add meta-information to the structure and how to use it

Tags are a way of attaching additional information to structure fields.

Go specifications inStructure type definitionDefine the label as

The field declaration can be followed by an optional string literal label, which becomes the attribute of all fields in the corresponding field declaration. An empty label string is equivalent to a missing label. These marks are visible through the reflection interface and participate in the type identification of the structure, but will be ignored in other cases.

And provide an example:

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" }

A real example

A common use case is when unmarshalling JSON, as I did inUse Go for JSON processing.

In that article, this example

type Member struct {
    Age       int `json:"age,string"`

telljson.Unmarshal()PutageJSON attributes, onestringAnd place it inAgeon siteMember,translate toint. In this example, we passjsonTwo pieces of information, separated by commas.

Label format

Label usekey:"value"format. This is not a strict rule, but a convention to provide built-in resolution. In this example, we only have one key-value pair, but we can have more than one:

type Member struct {
    Age       int `json:"age,string" xml:"the_age,string"`

By convention, the key is the name of the package we want to locate. In the example above,encoding/jsonwithencoding/xml.

But tags can also be used by ourselves in our own libraries, they are not only reserved for the standard library.

What is the use of labels?

Different software packages use tags for different reasons.

You will see that they are used in encoding libraries, such as json, xml, bson, yaml, and also in ORM/database libraries, and evenFill the structure with form data.

The following list was posted onand soAnd very comprehensive:

There are many other uses in the wild. E.gmcuadros / go-defaultsUse it to set default values for structure fields, andasaskevich / govalidatorAllows to add tags that confirm verification (this is just one possible method of verification,Alternatives exist).

fatih/gomodifytagsAllow editing of tags at runtime,

Use tags in code

To use tags in your own code, you can usereflectpackage.

Let's tryageIn the member structure. The following code

package main

import ( “fmt” “reflect” )

type Member struct { Age int something:"age" }

func main() { member := Member{34} t := reflect.TypeOf(member) field := t.Field(0) //field, _ := t.FieldByName(“Age”) //alternative fmt.Print(field.Tag.Get(“something”)) }


Will printage, Thanks tokey:"value"The format of the label.

More tutorials: