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 關注
- 140 瀏覽
添加回答
舉報