Skip to content

Instantly share code, notes, and snippets.

@AdamKalnas
Created March 6, 2014 16:37
Show Gist options
  • Save AdamKalnas/9393757 to your computer and use it in GitHub Desktop.
Save AdamKalnas/9393757 to your computer and use it in GitHub Desktop.
Duck Typing in GoLang - Lightning Talk Code
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