문법 : https://gobyexample.com/interfaces
package main
type Go interface {
testG() (string)
testO() (string)
}
type Duck struct {
}
func (d *Duck) testG() string{
return "asdf"
}
func (d *Duck) testO() string{
return "asdf"
}
func Test(a Go){
}
func main() {
aa := &Duck{}
Test(aa)
}
1 Test 함수에는 Go
라는 인터페이스 타입의 인자가 선언이 되어있다.
2 Duck 타입의 파라미터가 들어가진다.
3 Duck
구조체 타입이 Go
인터페이스 메소드를 모두 구현했다.
4 구현된 메소드 testG
또는 testO
둘 중 하나를 주석처리하면 오류가 발생한다.
즉, Go
인터페이스 메소드를 모두 구현한 구조체만이
Test 함수 파라미터로 들어갈 수 있다.