3
go 语言泛型多态和接口多态有什么区别
source link: https://www.v2ex.com/t/857646
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
今天面试被问到的,我答不上来,他问的是不是这两种写法的区别?
package main
import "fmt"
type Dog interface {
eat()
}
type dog struct{}
func (d dog) eat() {
fmt.Println("dog eat ...")
}
func dogEat1[T Dog](d T) {
d.eat()
}
func dogEat2(d Dog) {
d.eat()
}
func main() {
d := dog{}
dogEat1(d)
dogEat2(d)
}
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK