Reflection is an advanced Go feature that allows you to dynamically learn the type of an arbitrary object as well as information about its structure. You should recall that the dataStructures.go program from Chapter 2, Writing Programs in Go, used reflection to find out the fields of a data structure as well as the type of each fields. All of this happened with the help of the reflect Go package and the reflect.TypeOf() function that returns a Type variable.
Reflection is illustrated in the reflection.go Go program that will be presented in four parts.
The first one is the preamble of the Go program and has the following code:
package main import ( "fmt" "reflect" )
The second part is as follows:
func main() { type t1 int type t2 int x1 := t1(1) x2 := t2(1) x3 := 1
Here, you create two new types, named t1 and...