reflect.Type & reflect.Value
reflect.Type
TypeOf returns the reflection Type that represents the dynamic type of i. If i is a nil interface value, TypeOf returns nil.
1 | func TypeOf(i interface{}) Type { |
1 |
|
reflect.TypeOf 返回的是i 具体的类型。 所以w的类型是*os.File而不是io.Writer。
为方便进行debug,可以用fmt.Printf(“%T\n”, 3) 来代替 fmt.Println(reflect.TypeOf(3))。
reflect.Value
As with reflect.TypeOf, the results of reflect.ValueOf are always concrete. 和reflect.TypeOf一样,reflect.ValueOf返回也是一个具体的值。
1 | v := reflect.ValueOf(3) // a reflect.Value |
Kind
Although there are infinitely many types, there are only a finite number of kinds of type: the basic types Bool, String and all the numbers; the aggregate types Array and Struct, the reference types Chan, Func, Ptr, Slice and Map; interface types; and finally Invalid, meaning no value at all (The zero value of a reflect.Value has kind Invalid).
尽管具体的数据类型可以有无限种,但是它们可以被分为几种类型。这个就是reflect.Kind.
1 | // Any formats any value as a string. |
reference
- 《The Go Programming Lauguage》
- https://docs.hacknode.org/gopl-zh/ch12/ch12-02.html