package main import ( "fmt" "reflect" ) type Person struct { Name string Age int } func main() { p := Person{"Alice", 25} t := reflect.TypeOf(p) fmt.Println(t.Name()) // prints "Person" fmt.Println(t.Kind()) // prints "struct" }
package main import ( "fmt" "reflect" ) type Animal interface { Speak() string } type Dog struct { Name string } func (d Dog) Speak() string { return "woof" } func main() { a := Dog{"Fido"} t := reflect.TypeOf(a) if t.Implements(reflect.TypeOf((*Animal)(nil)).Elem()) { fmt.Println("a satisfies the Animal interface") } }In this example, we define an interface `Animal` with a single method `Speak`. We also define a struct `Dog` which implements this interface. We create an instance of `Dog` and use reflect.TypeOf to get its reflect.Type. We then check if the type implements the `Animal` interface using the Implements function. Package library: reflect.