GO comprehensive analysis json tag articles

In dealing with json format string, struct structure often see statement when the right side of the property there Backticks content enclosed. Shaped like:

1 type User struct {
2     UserId   int    `json:"user_id" bson:"user_id"`
3     UserName string `json:"user_name" bson:"user_name"`
4 }

struct member variable label (Tag) Description

To a more detailed understanding of this, we must first look at golang basis, in golang, the name is the recommended way is with a hump, and in the case of the first letter has a special grammatical meaning: can not be referenced outside the package. However, regular exchanges data with other systems, for example, converted into json format, stored mongodb ah like. If you use this time as a key attribute names may not necessarily meet the project requirements.

So the more the content of the anti-quotation marks, called tags (Tag) in golang, when converted into other data formats when certain fields which will be used as a key. For example, a format json Example turn into:

1 u := &User{UserId: 1, UserName: "tony"}
2 j, _ := json.Marshal(u)
3 fmt.Println(string(j))

// output content:

// {"user_id":1,"user_name":"tony"}

// If the label does not increase in the property description, the output:

{// "UserId": 1, "UserName": "Tony"}
// attribute name can be seen to do directly with the struct key.

// == bson which there is a statement that is used to store data == mongodb use

struct member variable label (Tag) Gets

So when we need to package some of their own operations, the need to use the content of Tag, Ze Yang went to get it? Here you can use the package reflection method (the reflect) is acquired:

1 t := reflect.TypeOf(u)
2 field := t.Elem().Field(0)
3 fmt.Println(field.Tag.Get("json"))
4 fmt.Println(field.Tag.Get("bson"))

The complete code is as follows:

 1 package main
 2  
 3 import (
 4     "encoding/json"
 5     "fmt"
 6     "reflect"
 7 )
 8  
 9 func main() {
10     type User struct {
11         UserId   int    `json:"user_id" bson:"user_id"`
12         UserName string `json:"user_name" bson:"user_name" `
 13      }
 14      // output format json 
15      U: = {the User & the UserId: . 1 , UserName: " Tony " }
 16      J, _: = json.Marshal (U)
 . 17      fmt.Println ( String (J))
 18 is      / / outputting content: { "user_id":. 1, "USER_NAME": "Tony"}
 . 19   
20 is      // tag for the content 
21 is      T: = reflect.TypeOf (U)
 22 is      Field: = t.Elem () Field, (. 0 )
 23      fmt.Println (field.Tag.Get ( " json "))
24     // Output: user_id 
25      fmt.Println (field.Tag.Get ( " BSON " ))
 26 is      // Output: user_id 
27 }

Custom tag

1 type User struct {
2     UserId   int    `json:"user_id" bson:"user_id" test:"test"`
3     UserName string `json:"user_name" bson:"user_name"`
4 }

Gets the value of the tag test

1  // Get the contents of tag 
2  typeof : = reflect.TypeOf (U)
 . 3 Field: = typeof .Elem () Field, (. 0 )
 . 4 fmt.Println (field.Tag.Get ( " JSON " ))
 . 5  / / output: user_id 
. 6 fmt.Println (field.Tag.Get ( " BSON " ))
 . 7  // output: user_id 
. 8 fmt.Println (field.Tag.Get ( " Test " ))
 . 9  // output: test

 

Guess you like

Origin www.cnblogs.com/lurenq/p/11533219.html