Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
调用将接口作为Go参数的函数_Go - Fatal编程技术网

调用将接口作为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(){}
这个

因此,即使没有,你也会被引导,可以猜出遗漏了什么。

请试着去看并偏爱接口示例,但是的,你需要仔细阅读,哟。