1 回答
TA贡献1811条经验 获得超4个赞
Foo
不执行fake
。如果您稍微扩展您的代码示例(在 Go 操场上尝试),这是显而易见的:
package main
import "fmt"
type fake interface {
getAge(valueInt int, valStr string) (age int, name string, err error)
}
type Foo struct {
name string
}
func (b *Foo) getAge(valueInt int, valStr string) (age int, retErr error) {
age = valueInt
return age, nil
}
func bar(f fake) {
_, name, _ := f.getAge(10, "")
}
func main() {
inst := &Foo{name: "foo"}
value, _ := inst.getAge(2, "foo")
fmt.Println(value)
bar(inst)
}
这会产生一个非常具有描述性的编译错误:
prog.go:28:5: cannot use inst (type *Foo) as type fake in argument to bar:
*Foo does not implement fake (wrong type for getAge method)
have getAge(int, string) (int, error)
want getAge(int, string) (int, string, error)
- 1 回答
- 0 关注
- 97 浏览
添加回答
举报