Skip to content

Instantly share code, notes, and snippets.

@fsouza
Created May 21, 2012 04:01
Show Gist options
  • Save fsouza/2760505 to your computer and use it in GitHub Desktop.
Save fsouza/2760505 to your computer and use it in GitHub Desktop.
Go duck typing
package main
import (
"fmt"
)
type Duck interface {
Quak()
Walk()
}
func QuakAndWalk(duck Duck) {
duck.Quak()
duck.Walk()
}
type Chicken struct{}
func (c *Chicken) Quak() {
fmt.Println("Chicken quaking")
}
func (c *Chicken) Walk() {
fmt.Println("Chicken walking")
}
func main() {
var c Duck
c = &Chicken{}
QuakAndWalk(c)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment