Skip to content

Instantly share code, notes, and snippets.

@weiofcn
Created December 6, 2017 06:56
Show Gist options
  • Save weiofcn/06123d5f897d24e247e58610b88963ce to your computer and use it in GitHub Desktop.
Save weiofcn/06123d5f897d24e247e58610b88963ce to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
type I interface {
M()
}
type T1 struct{}
func (T1) M() {
fmt.Println("T1.M")
}
type T2 struct{}
func (T2) M() {
fmt.Println("T2.M")
}
func f(i I) {
i.M()
}
func main() {
// 一个接口,可以满足多个类型
f(T1{}) // "T1.M"
f(T2{}) // "T2.M"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment