This is a creation in Article, where the information may have evolved or changed.
Before writing Java has already known the concept of reflection, read the article specifically introduced the Java reflection mechanism, but now forget.
Today, when writing a general function to judge the incoming parameters by the row type, but also to define the same type of variables as the row fetch operation, Baidu, Google,golang reflect package also looked for a long time, also did not see the return value to define a variable method. Finally get inspired in a blog post-–> click to see.
This article does not tell how to define a variable, but after reading it to think about, since to return a type, but the type is indeterminate, if there is a function to return the variable, then its return type must be interface, In the official website The return type is a function of interface, that is, it is a function of the value type, so you cannot use reflect. TypeOf (i interface{}) to operate, you can only use reflect. ValueOf (). So slowly combined with the appeal blog plus documentation, get~
package mainimport ( "reflect" "fmt")func main() { "tangs" fmt.Println("v2's value is : "", type is : ", reflect.TypeOf(v2)) ty := reflect.ValueOf(&v2).Elem() s := ty.Interface() "tangs" fmt.Println("s's value is : "", type is : " ,reflect.TypeOf(s))}
Result: