package main import ( "go/types" "fmt" ) type Person struct { Name string Age int } func main() { personType := types.NewStruct( []*types.Var{ types.NewVar(0, nil, "Name", types.TypeString), types.NewVar(0, nil, "Age", types.TypeInt), }, nil, ) fmt.Println(personType) }
package main import ( "go/types" "fmt" ) type Employee struct { Name string Age int } func main() { var employeeType = types.NewStruct( []*types.Var{ types.NewVar(0, nil, "Name", types.TypeString), types.NewVar(0, nil, "Age", types.TypeInt), }, nil, ) for i := 0; i < employeeType.NumFields(); i++ { fmt.Println(employeeType.Field(i).Name()) } }In this example, the NumFields function is used to determine the number of fields in the Employee struct type. The Field function is then used to get each field one at a time and print its name to the console. In summary, the go.tools.go.types package provides a set of tools for working with Go types. Its functions can be used to define new types, inspect existing types, and perform a variety of other type-related operations. Overall, the go.tools.go.types package is a valuable library for anyone working with Go coding.