Skip to content

Instantly share code, notes, and snippets.

@tetsuok
Created May 26, 2012 20:03
Show Gist options
  • Select an option

  • Save tetsuok/2795124 to your computer and use it in GitHub Desktop.

Select an option

Save tetsuok/2795124 to your computer and use it in GitHub Desktop.
A sample of duck typing
// duck typing
package main
import "fmt"
// Virtual duck
type Duck interface {
Quack() string
}
type Person struct {
name string
}
type ActualDuck struct{}
func (p Person) Quack() string {
return "I am " + p.name
}
func (d ActualDuck) Quack() string {
return "Quaaack"
}
func test(d Duck) {
fmt.Println(d.Quack())
}
func main() {
test(Person{"John"})
test(ActualDuck{})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment