Created
January 14, 2016 06:39
-
-
Save MakoTano/88268ec44da3eb778500 to your computer and use it in GitHub Desktop.
GoInAction Chapter5 - Interface 説明その1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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