Why Go Interface is better than Java and C#

Go language Interface

Accept interfaces, return structs

Sample code

// animal.gotype Animal interface{
Speak()
Run()
}
type Dog struct{}
func (Dog) Speak(){fmt.Println("bowwow")}
func (Dog) Run(){fmt.Println("yay,I'm running")}
type Cat struct{}
func (Cat) Speak(){fmt.Println("mewmew")}
func (Cat) Run(){fmt.Println("shut up ****er")}
// main.gotype Runner interface{
Run()
}
func main(){
Race([]Runner{animal.Dog{}, animal.Cat{}})
}
func Race(runners []Runner){
for _,runner :=range runners{
runner.Run()
}
}
// convert.gofunc Save(f *os.File,doc *Document) error
// os.gopackage os
type File interface{
Read([]byte) (int,error)
Write([]byte) (int,error)
Seek(int64,int) (int64,error)
Close()error
}
//Save はDocumentの内容をfに書き込む
func Save(f os.File,doc *Document) error
// os.gopackage os
type File interface{
Reader
Writer
Seeker
Closer
}
func Save(f os.Writer,doc *Document) error

Accept interfaces

// convert.gotype Writer interface{
Write([]byte) (int,error)
}
func Save(w Writer,doc *Document) error

Return structs

Problems with duck typing

var _ Interface = (*Impl)(nil)

Go interface

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store