package main import ( "fmt" "go/types" ) func main() { var x = 42 fmt.Println(types.TypeOf(x)) }
package main import ( "fmt" "go/types" ) func main() { var x int32 = 42 var y int64 = 42 fmt.Println(types.Identical(types.TypeOf(x), types.TypeOf(y))) }
package main import ( "fmt" "go/types" ) func main() { var x = 42 obj := types.ObjectOf(types.NewPackage("main", ""), "x") fmt.Println(types.TypeOf(obj.Type())) }This example shows how to use the ObjectOf function to get the type of a variable declared in a package. The output will be "int". Overall, the go/types package is a useful library for Go developers who need to analyze and manipulate Go types in their programs.