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.