package main
import (
"fmt"
)
type animal interface {
speak()
}
type dog struct {
name, sound string
}
type cat struct {
name, sound string
}
func (d dog) speak() {
fmt.Println(d.name, " goes ", d.sound)
}
func (c cat) speak() {
fmt.Println(c.name, " goes ", c.sound)
}
func animal_speak(a animal) {
fmt.Println(a.speak())
}
func main() {
dogo := dog{"scooby", "woof"}
cato := cat{"garfield", "meow"}
animal_speak(dogo)
animal_speak(cato)
}
当我调用动物界面时,出现以下错误
我究竟做错了什么?
Link to playground
最佳答案
该接口(interface)不用作值。您正在使用一个不返回任何值的函数调用。speak()
不返回任何内容...那么您希望它打印什么?
关于go - golang接口(interface) “used as value”错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47977818/