package main import ( "fmt" "reflect" ) func main() { var x int64 = 42 fmt.Println(reflect.TypeOf(x).Kind()) // Output: int64 }
package main import ( "fmt" "reflect" ) type Person struct { Name string Age int } func main() { p := Person{Name: "John", Age: 30} if reflect.ValueOf(p).Kind() == reflect.Struct { fmt.Println("p is a struct") } else { fmt.Println("p is not a struct") } // Output: p is a struct }
package main import ( "fmt" "reflect" ) type Animal interface { Speak() string } type Dog struct{} func (d Dog) Speak() string { return "Woof!" } func main() { var a Animal = Dog{} fmt.Println(reflect.TypeOf(a).Elem().Kind()) // Output: struct }In this example, we define an interface `Animal` and a struct `Dog` that implements the `Speak()` method. We then create a variable `a` of type `Animal` initialized with a `Dog` object. We use the reflect library to get the underlying type of the interface, which will be the struct `Dog`. Package Library: golang.org/x/tools