Golang in the struct about reflection tag

Source: Internet
Author: User
Tags mongodb support
This is a created article in which the information may have evolved or changed.
Package Mainimport ("FMT" "reflect") type user struct {name string ' Json:name-field ' age  int}func main () {User: = &u ser{"John Doe the Fourth", 20}field, OK: = reflect. TypeOf (user). Elem (). Fieldbyname ("name") if!ok {panic ("Field not Found")}fmt. Println (Getstructtag (field))}func Getstructtag (f reflect. Structfield) string {return string (F.tag)}

Output

Json:name-field


The structure domain can be obtained directly from the reflection, and the tag in the domain can be called to get the tag to be processed. In Golang, such as the well-known Xorm package are not added to MongoDB support. It is difficult to use Golang's struct to support JSON well, most of them rely on tag. It is still very necessary to use reflection at the bottom to get tag content.

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.