12.2 reflect.Type
and reflect.Value
Reflection is provided by the reflect
package.
It defines two important types, Type
and Value
.
A Type
represents a Go type.
It is an interface with many methods for discriminating among types
and inspecting their components, like the fields of a struct or the
parameters of a function.
The sole implementation of reflect.Type
is the type
descriptor (§7.5), the same entity that identifies the
dynamic type of an interface value.
The reflect.TypeOf
function accepts any interface{}
and returns its dynamic type as a reflect.Type
:
t := reflect.TypeOf(3) // a reflect.Type fmt.Println(t.String()) // "int" fmt.Println(t) // "int"
The TypeOf(3)
call above assigns the value 3
to the ...
Get The Go Programming Language now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.