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}