3

go 语言泛型多态和接口多态有什么区别

 1 year ago
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.

V2EX  ›  Go 编程语言

go 语言泛型多态和接口多态有什么区别

  v2defy · 4 小时 12 分钟前 · 813 次点击

今天面试被问到的,我答不上来,他问的是不是这两种写法的区别?


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)
}



About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK