Golang 结构体标签对于Go本身来说就是注释,但是这个字符串可以通过reflect API访问到,因此不同的包可能会赋予自己的含义。
具体你看看encoding/xml和encoding/json的文档。
例如:
type Config struct {Log string `myConf:"base:log"` //myConf为自定义的结构体}
在Go语言当中,好多功能都使用了结构体标签。主要应用在encoding/json和encoding/xml之中。那么我们先用Json的使用做一个例子。首先创建一个结构体。
type User struct {Id int `json:"id"`Name string `json:"name"`Bio string `json:"about,omitempty"`Active bool `json:"active"`Admin bool `json:"-"`CreatedAt time.Time `json:"created_at"`}
对于结构体的标签就是紧跟成员定义后,类型后面的那串字符串,就是标签。其中出现的这个是Golang为了保持源字符串的意思。这个结构体标签,在编码后,Json中的Key值就会被响应的tag个中json后面的名称代替,在此使用的是Go语言的包反射机理获取标签再填充为Json,具体我们下文再说。在没有使用结构体表情的情况下,我们使用json.Marshal进行处理后输出的是这样的:
{"Id": 1,"Name": "John Doe","Bio": "Some Text","Active": true,"Admin": false,"CreatedAt": "2016-07-16T15:32:17.957714799Z"}
和结构体所定义的结构成员的名称是一致的,当我们使用结构体标签后:
{"id": 1,"name": "John Doe","about": "Some Text","active": true,"created_at": "2016-07-16T15:32:17.957714799Z"}
可以很清晰的看到,结构体标签上的json后面的那段字符串,成为了Json中的Key值。在此我们查看Go语言中的Json和XML包源码,是很清晰的可以看到这一点的。对于自己开发获取,我们可以使用编程中的包反射进行获取结构体的标签名
package mainimport ("fmt""reflect")// Name of the struct tag used in examplesconst tagName = "validate"type User struct {Id int `validate:"-"`Name string `validate:"presence,min=2,max=32"`Email string `validate:"email,required"`}func main() {user := User{Id: 1,Name: "John Doe",Email: "john@example",}// TypeOf returns the reflection Type that represents the dynamic type of variable.// If variable is a nil interface value, TypeOf returns nil.t := reflect.TypeOf(user)// Get the type and kind of our user variablefmt.Println("Type:", t.Name())fmt.Println("Kind:", t.Kind())// Iterate over all available fields and read the tag valuefor i := 0; i < t.NumField(); i++ {// Get the field, returns https://golang.org/pkg/reflect/#StructFieldfield := t.Field(i)// Get the field tag valuetag := field.Tag.Get(tagName)fmt.Printf("%d. %v (%v), tag: '%v'\n", i+1, field.Name, field.Type.Name(), tag)}}
其中首先导入了reflect这个包,然后通过reflect中的TypeOf,将user结构体变成动态变量。然后遍历结构体程序,通过.Tag.Get方法获取结构体的每一个成员的标签,然后去做我们想做的事情~
Type: UserKind: struct1. Id (int), tag: '-'2. Name (string), tag: 'presence,min=2,max=32'3. Email (string), tag: 'email,required'
看完下面的例子你就明白啦,这个例子,解析时可以把json中name解析成struct中的Name(大小写不一样),把emailAddress解析成Email(名字都不一样):
package mainimport ("encoding/json""fmt")// Name of the struct tag used in examplestype User struct {Id int `json:"id"`Name string `json:"name"`Email string `json:"emailAddress"`}func main() {u := &User{}// str := []byte(`{"name":"lee","id":5266, "mail":"test@163.com"}`) //这个不能导出mail,因为mail不是`Email`, 也不是`emailAddress`,所以不能导出str := []byte(`{"name":"lee","id":5266, "emailAddress":"test@163.com"}`)if err := json.Unmarshal(str, u); err != nil {return}fmt.Println(u)}