interface
行为定义 / Behavior Definition
go
type Speaker interface {
Speak() string
}类型判断 / Type Assertion & Type Switch
go
var s Speaker = Person{Name: "Bob"}
if p, ok := s.(Person); ok {
fmt.Println("It's a Person:", p.Name)
}
switch v := s.(type) {
case Person:
fmt.Println("Person:", v.Name)
default:
fmt.Println("Unknown type")
}