Created
December 6, 2017 06:56
-
-
Save weiofcn/06123d5f897d24e247e58610b88963ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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