import "reflect" func getType(a interface{}) string { return reflect.TypeOf(a).String() } func main() { x := 42 fmt.Println(getType(x)) //prints "int" }
import ( "reflect" "fmt" ) type Person struct { Name string Age int } func main() { pType := reflect.TypeOf(Person{}) pVal := reflect.New(pType).Elem() pVal.FieldByName("Name").SetString("Alice") pVal.FieldByName("Age").SetInt(25) p := pVal.Interface().(Person) fmt.Println(p) //prints "{Alice 25}" }In this example, we use the reflect Type package to create a new instance of the Person struct, set its fields, and then convert it back to a Person object. Overall, the Go reflect Type package provides a powerful way to manipulate and examine object types at runtime, making it a valuable tool for many Go programming tasks.