2014-09-17 2 views
-5

인터페이스를 함수의 매개 변수로 어떻게 구성합니까? 당신이 "여기에서"아무것도 넣어하려고하면Go에서 인터페이스를 매개 변수로 사용하는 함수 호출

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

func blah (i blahinterface) { 

} 


blah(?) < what goes in here 
+2

[유효 이동] (http://golang.org/doc/effective_go.html) 및 [이동 둘러보기] (http://tour.golang.org/) – JimB

+1

[ Go Wikipedia article] (http://en.wikipedia.org/wiki/Go_(프로그래밍 _ 언어) #Interface_system) 인터페이스 예를 들어 보았지만 읽을 필요가 있습니다. – twotwotwo

답변

2

사실, 컴파일러가없는 것을 정확하게 당신을 말할 것이다 :

type S struct{} 

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

go buildon this example 당신에게 말할 것이다 :

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(){} 

program do compile just fine.

그럼에도 불구하고 reading about interfaces이 없어도 무엇이 빠졌는지 알 수 있습니다.

관련 문제