Go Language Practical Notes (25) | Go Struct Tag

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

"Go language Combat" reading notes, not to be continued, welcome to sweep code attention flysnow_org to the public or website http://www.flysnow.org/, the first time to see follow-up notes. If you feel helpful, share it with your friends and thank you for your support.

In the previous introduction to go reflection, mentioned how to get the tag of the struct by reflection, this article is mainly to introduce the use and principle of this, before we introduce the JSON string and struct type to convert each other example.

JSON string Object Conversion

123456789101112131415
func Main ()  {var u userh:=' {' name ': ' Zhang San ', ' age ': ' Err:=json} '. Unmarshal ([]byte(h), &u)if err!=nil{fmt. PRINTLN (ERR)}else {fmt. PRINTLN (U)}}typestructstring' name 'int' age '}

The above example is the JSON string to the user object example, here is the main use of the user structure corresponding to the field Tag,json parsing principle is to get the tag of each field by reflection, and then the parsed JSON corresponding value assigned to them.

The field tag can not only convert the JSON string to a struct object, but also convert the struct object to a JSON string.

12
Newjson,err:=json. Marshal (&u) fmt. Println ((string(Newjson)))

Then just the example, so that it can be converted into a new string, through the printout, you can see and start the input of the same string.

Reflection Get Field Tag

The tag of the field is labeled on the field, so we can get the field first, then the tag on the field.

12345678910
func Main ()  {var u usert:=reflect. TypeOf (U) for i:=0; I<t.numfield (); I++{sf:=t.field (i) fmt. Println (SF. TAG)}}

Get field The previous article we mentioned, after getting the field, the call .Tag gets to the corresponding tag field.

Key value pairs for the field tag

Most of the time, one of our structs has more than one function, such as we need JSON for mutual transfer, also need Bson and ORM parsing, so a field may correspond to a number of different tags, in order to meet the different functional scenarios.

Go Struct provides us with a tag for key-value pairs to meet our needs.

12345678910111213141516
func Main ()  {var u usert:=reflect. TypeOf (U) for i:=0; I<t.numfield (); I++{sf:=t.field (i) fmt. Println (SF. Tag.get ("JSON")}}}typestructstring' json: ' name ' 'int  ' JSON:' age ' '}

In the example above, the struct Tag,key-value is configured using a key-value pair separated by a colon, where the key is json , so we can get the corresponding value by this key, that is, through the .Tag.Get("json")) method. Getthe method is to get the corresponding tag settings via a key.

In addition, we can also set up multiple keys to meet the above-mentioned scenarios.

12345678910111213141516
func Main ()  {var u usert:=reflect. TypeOf (U) for i:=0; I<t.numfield (); I++{sf:=t.field (i) fmt. Println (SF. Tag.get ("JSON"),",", SF. Tag.get ("Bson")}}}typestructstring' json: ' Name ' Bson: ' B_name '  int' json: ' Age ' Bson: ' B_age'}

Multiple keys are separated by spaces, and then use the Get method to get the values of different keys.

struct tag can provide a string-to-struct mapping capability so that we can do the conversion, but also as a field metadata configuration, providing the configuration we need, such as the generation of swagger documents.

"Go language Combat" reading notes, not to be continued, welcome to sweep code attention flysnow_org to the public or website http://www.flysnow.org/, the first time to see follow-up notes. If you feel helpful, share it with your friends and thank you for your support.

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.