在Go语言当中,好多功能都使用了结构体标签。主要应用在encoding/json和encoding/xml之中。那么我们先用Json的使用做一个例子。首先创建一个结构体。
1 2 3 4 5 6 7 8 |
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进行处理后输出的是这样的:
1 2 3 4 5 6 7 8 |
{ "Id": 1, "Name": "John Doe", "Bio": "Some Text", "Active": true, "Admin": false, "CreatedAt": "2016-07-16T15:32:17.957714799Z" } |
和结构体所定义的结构成员的名称是一致的,当我们使用结构体标签后:
1 2 3 4 5 6 7 |
{ "id": 1, "name": "John Doe", "about": "Some Text", "active": true, "created_at": "2016-07-16T15:32:17.957714799Z" } |
可以很清晰的看到,结构体标签上的json后面的那段字符串,成为了Json中的Key值。在此我们查看Go语言中的Json和XML包源码,是很清晰的可以看到这一点的。对于自己开发获取,我们可以使用编程中的包反射进行获取结构体的标签名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package main import ( "fmt" "reflect" ) // Name of the struct tag used in examples const 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", } // 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 variable fmt.Println("Type:", t.Name()) fmt.Println("Kind:", t.Kind()) // Iterate over all available fields and read the tag value for i := 0; i < t.NumField(); i++ { // Get the field, returns https://golang.org/pkg/reflect/#StructField field := t.Field(i) // Get the field tag value tag := 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方法获取结构体的每一个成员的标签,然后去做我们想做的事情~
1 2 3 4 5 |
Type: User Kind: struct 1. Id (int), tag: '-' 2. Name (string), tag: 'presence,min=2,max=32' 3. Email (string), tag: 'email,required' |
原文参考:https://sosedoff.com/2016/07/16/golang-struct-tags.html