This is a creation in Article, where the information may have evolved or changed.
Go language also has the reflection mechanism, today self-study to Go's reflection, the discovery still is worth to record some this knowledge point. Go language It is done through the two interface methods of the reflect package, respectively:
Reflect. TypeOf (i interface{}) and reflect. ValueOf (i interface{}), this time we find that the parameter is an empty interface, that is, you can receive any parameter, because all variables are implemented by default interface{}, which is a bit like the parent class of all Java classes are object. is divided into type and value when obtaining a type of the current parameter. Type is the kind that is declared at the time of the static declaration, and value is what is paid in the code. Go reflection is powerful, and there is a kind under each type or value, and this kind represents the underlying true data type of the runtime's current parameter or the state of the object. For example: type x int var count X reflect. TypeOf (count) gets the main. X. Reflect. TypeOf (count). Kind gets the Int. Reflect. ValueOf (count) gets the 0. Reflect. ValueOf (count). Kind gets the Int. The kind function is what the type or value of the underlying runtime concrete data types are!!! Here are some examples of exercises I do:
Package Main
Import (
"Reflect"
"FMT"
)
Type people struct {
Name string
Age int
Height int
}
Type Student struct {
People people
}
var people people = people{"Julian", 26, 175}
var mstudent Student = student{people}
Type Loadingview Interface {
Loadsucceed ()
Loadfailure ()
}
Func (People People) loadsucceed () {
}
Func (People People) loadfailure () {
}
Func testLoad (load Loadingview) {
Loadtype: = reflect. TypeOf (load)
Fmt. Print ("The type of load is:--", Loadtype)
Fmt. Print ("\ n")
Loadtypekind: = Loadtype.kind ()
Fmt. Print ("Loadtype's Kind is:--", Loadtypekind)
Fmt. Print ("\ n")
Loadvalue: = reflect. ValueOf (load)
Fmt. Print ("The value of load is:--", Loadvalue)
Fmt. Print ("\ n")
Loadvaluekind: = Loadvalue.kind ()
Fmt. Print ("Kind of the value of load is:--", Loadvaluekind)
Fmt. Print ("\ n")
Switch value: = load. (type) {
Case people:
Fmt. Print (value)
}
}
Func Main () {
Peopletype: = reflect. TypeOf (mstudent)
Fmt. Print ("People type is:--", Peopletype)
Fmt. Print ("\ n")
Peopletypekind: = Peopletype.kind ()
Fmt. Print ("Peopletype's Kind is:--", Peopletypekind)
Fmt. Print ("\ n")
Peoplevalue: = reflect. ValueOf (mstudent)
Fmt. Print ("People's value is:--", Peoplevalue)
Fmt. Print ("\ n")
Peoplevaluekind: = Peoplevalue.kind ()
Fmt. Print ("The kind of people's value is:--", Peoplevaluekind)
Fmt. Print ("\ n")
TestLoad (People)
}
Here is the result of the printout:
The type of people is:--main. People
Peopletype's kind is:--struct
The value of people is:--{julian 26 175}
The kind of people's value is:--struct
The type of load is:--main. People
Loadtype's kind is:--struct
The value of load is:--{julian 26 175}
The kind of the value of load is:--struct
{Julian 26 175}