Skip to content

Instantly share code, notes, and snippets.

@MakoTano
Created January 14, 2016 06:39
Show Gist options
  • Save MakoTano/88268ec44da3eb778500 to your computer and use it in GitHub Desktop.
Save MakoTano/88268ec44da3eb778500 to your computer and use it in GitHub Desktop.
GoInAction Chapter5 - Interface 説明その1
package main
import "fmt"
type Dog interface {
Run() error
}
// ---
type MyDog struct{}
func (d *MyDog) Run() error {
fmt.Println("my dog is runnig!")
return nil
} // => interfaceがもつ振る舞いの定義に応じた処理
// ---
type MyCat struct{}
// ---
func run(d Dog) {
d.Run()
}
func main() {
myDog := MyDog{}
run(myDog) // => interfaceが一致しているので代入できる
myCat := MyCat{}
run(myCat) // => prog.go:30: cannot use myCat (type MyCat) as type Dog in argument to run: MyCat does not implement Dog (missing Run method)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment