如何使用结构字段的标签?

how tags of struct fields are used?

我不明白 struct 中的字段标签是如何使用的以及何时使用。每 https://golang.org/ref/spec#Struct_types:

[tag] becomes an attribute for all the fields in the corresponding field declaration

这是什么意思?

[tag] becomes an attribute for all the fields in the corresponding field declaration

除了上面的链接(What are the use(s) for tags in Go?", "go lang, struct: what is the third parameter") this thread 提供了一个示例,它不是 来自标准包:

Suppose I have the following code (see below). I use a type switch the check the type of the argument in WhichOne().

How do I access the tag ("blah" in both cases)?
I'm I forced the use the reflection package, or can this also be done in "pure" Go?

type PersonAge struct {
        name string "blah"
        age  int
}

type PersonShoe struct {
        name     string "blah"
        shoesize int
}

func WhichOne(x interface{}) {
    ...
}

After reading that (again), looking in json/encode.go and some trial and error, I have found a solution.
To print out "blah" of the following structure:

type PersonAge struct {
        name string "blah"
        age  int
}

You'll need:

func WhichOne(i interface{}) {
        switch t := reflect.NewValue(i).(type) {
        case *reflect.PtrValue:
                x := t.Elem().Type().(*reflect.StructType).Field(0).Tag
                println(x)
        }  
}

此处:Field(0).Tag说明“成为相应字段声明中所有字段的属性”。