Golang Structtag Description

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

When working with JSON-formatted strings, it is often seen that when declaring a struct structure, the right side of the property is also surrounded by millet dots. Shaped like

Type User struct {    UserId   int    ' JSON: ' user_id ' Bson: ' user_id ' '    UserName string ' JSON: ' user_name ' Bson: "User_name" '}

struct member variable label (TAG) description

To more detailed understanding of this, to understand the basis of the Golang, in the Golang, the name is recommended is the hump way, and in the first letter case has a special grammatical meaning: Outside the package can not be referenced. However, it is often necessary to interact with other systems, such as turning into JSON format, storing to MongoDB, and so on. At this point, using the property name as the key value may not necessarily conform to the project requirements.

So it's just a little bit more. The content of millet dot, called tag in Golang, when converted to other data format, will use the specific field as the key value. For example, the previous example turns into JSON format:

u := &User{UserId: 1, UserName: "tony"}

J, _: = json. Marshal (U)

Fmt. Println (String (j))

Output content: {"user_id": 1, "user_name": "Tony"}

If the label description is not added in the attribute, the output is:

{"UserId": 1, "UserName": "Tony"}

You can see that the key value is made directly from the struct's property name.

There is also a Bson statement, which is used to store data in MongoDB.

struct member variable tag (tag) gets

So when we need to encapsulate some of the operations, need to use the contents of the tag, how to get it? This way you can use the methods in the reflection package (reflect) to get:

T: = reflect. TypeOf (U) field:= T.elem (). Field (0) fmt. Println (field. Tag.get ("JSON")) fmt. Println (field. Tag.get ("Bson"))

The complete code is as follows:

 PackageMainImport (    "Encoding/json" "FMT" "reflect") Func main () {type User struct {UserIdint' JSON: ' user_id ' Bson: ' user_id '' UserName string ' JSON:"User_name" Bson: "user_name"`    }    //Output JSON formatU: = &user{userid:1, UserName: "Tony"} J, _:=JSON. Marshal (U) fmt. Println (String (j))//output content: {"user_id": 1, "user_name": "Tony"}//get the contents of the tagT: =reflect. TypeOf (U) field:= T.elem (). Field (0) fmt. Println (field. Tag.get ("JSON"))    //Output: user_idFmt. Println (field. Tag.get ("Bson"))    //Output: user_id

The Beego ORM also defines the parameters by tag.

Package Import  (   "FMT" "reflect") type Job struct {alarmstatus         *string        ' json: ' alarm_status ' name: ' alarm_status ''   cputopology      string        ' JSON:' Cpu_topology "Name:" Cpu_topology "}  Func main () {   A:=" abc "   s:= Job{&a," Hello "}   st:= reflect. TypeOf (s)   field:= St. Field (1)   FMT. Println (field. Tag.get ("JSON"), field. Tag.get ("name"))}
 PackageMainImport (    "FMT" "reflect"//The reflect module is introduced here) type User struct {Name string"User Name"//This is the tag inside the quote.PASSWD string "User Passsword"} func Main () {User:= &user{"Chronos", "Pass"} s:= Reflect. TypeOf (user). Elem ()//get the type definition by reflection     forI: = 0; I < S.numfield (); i++{fmt. Println (S.field (i). TAG)//output the tag.    }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.