调用将接口作为Go参数的函数
如何将接口构造为函数的参数调用将接口作为Go参数的函数,go,Go,如何将接口构造为函数的参数 type blahinterface interface { method1() method2() method3() } func blah (i blahinterface) { } blah(?) < what goes in here 实际上,如果您尝试在这里输入任何内容,编译器将准确地告诉您缺少什么: type S struct{} func main() { fmt.Println("Hello, pl
type blahinterface interface {
method1()
method2()
method3()
}
func blah (i blahinterface) {
}
blah(?) < what goes in here
实际上,如果您尝试在这里输入任何内容,编译器将准确地告诉您缺少什么:
type S struct{}
func main() {
fmt.Println("Hello, playground")
s := &S{}
blah(s)
}
go构建将告诉您:
prog.go:20: cannot use s (type *S) as type blahinterface in argument to blah:
*S does not implement blahinterface (missing method1 method)
[process exited with non-zero status]
但是:
func (s *S) method1(){}
func (s *S) method2(){}
func (s *S) method3(){}
这个
因此,即使没有,你也会被引导,可以猜出遗漏了什么。请试着去看并偏爱接口示例,但是的,你需要仔细阅读,哟。