Created
March 6, 2014 16:37
-
-
Save AdamKalnas/9393757 to your computer and use it in GitHub Desktop.
Duck Typing in GoLang - Lightning Talk Code
This file contains 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 Animal interface { | |
sound() string | |
getName() string | |
} | |
func makeSound(a Animal) string { | |
return a.sound() | |
} | |
type Dog struct { | |
name string | |
} | |
func (d *Dog) sound() string { | |
return "Woof!" | |
} | |
func (d *Dog) getName() string { | |
return d.name | |
} | |
type Duck struct { | |
name string | |
} | |
func (d *Duck) sound() string { | |
return "Quack!" | |
} | |
func (d *Duck) getName() string { | |
return d.name | |
} | |
func main() { | |
var dog *Dog = new(Dog) | |
var duck *Duck = new(Duck) | |
dog.name = "Brewster" | |
duck.name = "Donald" | |
fmt.Println(makeSound(dog)) | |
fmt.Println(makeSound(duck)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment