Go struct Tag

Source: Internet
Author: User

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.

= = Sample code:

Package Mainimport ("Encoding/json"    "FMT"    "reflect") Type Userstruct{UserIdint' JSON:"user_id"' UserNamestring' JSON:"user_name"'}func main () {//Output JSON formatU: = &user{userid:1, UserName:"Alex"} J, _:=JSON. Marshal (U) fmt. Println (string(j))//output content: {"user_id": 1, "user_name": "Alex"}//get the contents of the tagT: =reflect. TypeOf (U) field:= T.elem (). Field (0) Field2:= T.elem (). Field (1) fmt. Println (field. Tag.get ("JSON"))    //output content: user_idFmt. Println (Field2. Tag.get ("JSON"))    //output content: User_name}

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.