I'm porting a program from go/ast
to reflect
. In order to pass the tests I need to get not only the top type information but also the underlying type if the underlying type is not built-in.
In the example below, is it possible for a program to know that the underlying type of main.T is main.TT?
package main
import "fmt"
import "reflect"
func main() {
type TT int
type T TT
x := T(0)
fmt.Println(reflect.TypeOf(x))
}
Output:
main.T
The underlying type of main.T
is int
, not main.TT
. The reflect package has no knowledge that main.T
was declared with main.TT
.
Here's what the specification has to say about underlying types:
Each type T has an underlying type: If T is one of the predeclared boolean, numeric, or string types, or a type literal, the corresponding underlying type is T itself. Otherwise, T's underlying type is the underlying type of the type to which T refers in its type declaration.
type T1 string type T2 T1 type T3 []T1 type T4 T3
The underlying type of string, T1, and T2 is string. The underlying type of []T1, T3, and T4 is []T1.