This is a created article in which the information may have evolved or changed.
The reflection mechanism is basically similar to Java, which can get information such as the type value of a struct or variable. When you get or modify the value of an object, be sure to determine whether the property is operational or it will cause an error.
The sample code is as follows:
Package Mainimport ("FMT" "reflect")///initial uppercase indicates that the field can be exported and can modify the value type User struct {ID intname string//not exported age int} Func (U User) Info () {fmt. Println (U)}func GetInfo (o interface{}) {t: = reflect. TypeOf (o) fmt. Println (T.name ()) V: = reflect. ValueOf (o) for I: = 0; I < T.numfield (); i++ {f: = T.field (i) fmt. Printf ("%s\n", F.name) if V.field (i). Caninterface () {///Here name is not exported, so when getting interface it will prompt for error val: = V.field (i). Interface () fmt. Printf ("%s:%v=%v\n", F.name, F.type, Val)}}}func main () {user1: = user{222, "Charles", 100}getinfo (User1)}