1 Introduction
Go struct declaration allows the field that came Tag
to do some mark on the field.
This Tag
is not just a simple string, because it is mainly used for reflection scenario, reflect
the packet is provided an operation Tag
method, the Tag
wording must follow certain rules.
2. Tag nature of
2.1 Tag Rules
Tag
Itself is a string, but the string is: 以空格分隔的 key:value 对
.
key
: Be non-empty string, the string can not contain control characters, space, quotation mark, colon.value
: String double quotation marks- Note: You can not have spaces before and after the colon
As shown in the following code, so writing has no real meaning, for illustration only Tag
rule
type Server struct {
ServerName string `key1: "value1" key11:"value11"`
ServerIP string `key2: "value2"`
}
The code ServerName
field Tag
contains two key-value pair. ServerIP
Field Tag
contains only a key-value pair.
2.2 Tag is part of Struct
As mentioned above, Tag
useful only in the reflective scene, reflected package provides an operating Tag
method. Before said method, it is necessary to look at how to manage Go struct field.
The following is a reflect
type declaration package, part of the field is omitted herein independent.
// A StructField describes a single field in a struct.
type StructField struct {
// Name is the field name.
Name string
...
Type Type // field type
Tag StructTag // field tag string
...
}
type StructTag string
Visible, a description of the structure member contains structure StructTag
, which itself is a string
. That Tag
is actually an integral part of the structure of the field.
2.3 Gets Tag
StructTag
It provides a Get(key string) string
method to obtain Tag
, for example:
package main
import (
"reflect"
"fmt"
)
type Server struct {
ServerName string `key1:"value1" key11:"value11"`
ServerIP string `key2:"value2"`
}
func main() {
s := Server{}
st := reflect.TypeOf(s)
field1 := st.Field(0)
fmt.Printf("key1:%v\n", field1.Tag.Get("key1"))
fmt.Printf("key11:%v\n", field1.Tag.Get("key11"))
filed2 := st.Field(1)
fmt.Printf("key2:%v\n", filed2.Tag.Get("key2"))
}
Program output is as follows:
key1:value1
key11:value11
key2:value2
3. Tag the meaning of existence
Tag examples in this article do not have any real meaning, which is to elaborate tag definitions and methods of operation, and to avoid before you ever seen such json:xxx
confusion.
Using reflection can be dynamically assigned to the structure members, it is because of tag, you can use the tag to determine the action assignment before the assignment.
For example, the official encoding/json
packet may be a JSON data Unmarshal
into a structure, the use of this process to the Tag. This package defines rules, as long as the tag is provided with reference to the rule can be converted into a different data structure of JSON.
In short: It is based on the characteristics of a struct tag, only such json, orm, etc. applications. Understanding this relationship is critical. Perhaps, you can define another tag rules, to handle your specific data.
4. Tag common usage
Common tag usage, mainly JSON data analysis, ORM mapping. ====