-6

How do you construct an interface as a parameter to a function?

type blahinterface interface {
    method1()
    method2()
    method3()
}

func blah (i blahinterface) {

}


blah(?)  < what goes in here
JimB
  • 104,193
  • 13
  • 262
  • 255
llSourcell
  • 29
  • 1
  • 2
  • 2
    Please try to go though [Effective Go](http://golang.org/doc/effective_go.html) and [A Tour of Go](http://tour.golang.org/) – JimB Sep 17 '14 at 19:09
  • 1
    Partial to [the Go Wikipedia article's](http://en.wikipedia.org/wiki/Go_(programming_language)#Interface_system) interfaces example, but yeah, you need to read up, yo. – twotwotwo Sep 17 '14 at 19:33

1 Answers1

2

Actually, if you try to put anything "in here", the compiler will tell you precisely what is missing:

type S struct{}

func main() {
    fmt.Println("Hello, playground")
    s := &S{}
    blah(s)
}

A go build on this example would tell you:

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]

But with:

func (s *S) method1(){}
func (s *S) method2(){}
func (s *S) method3(){}

The program do compile just fine.

So even without reading about interfaces, you are guided and can guess what is missing.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250