Skip to content

Instantly share code, notes, and snippets.

@jochasinga
Created October 1, 2014 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jochasinga/9f9eee05b648aea04564 to your computer and use it in GitHub Desktop.
Save jochasinga/9f9eee05b648aea04564 to your computer and use it in GitHub Desktop.
Abstracting a method to an interface
package main
import "fmt"
type Greeter interface {
Greet()
}
type Man struct {
language string
// other factors to decide if the girl would like you (for fun)
name, nationality, build, eye_color string
age, height uint8
// you could be in debt, so we can't rule out negative values
income int64
// this is to remind her you are available
married bool
}
func (m Man) Greet() {
switch m.language {
case "British" : fmt.Println("Good morning, dear")
case "American": fmt.Println("Mornin' there")
case "Japanese": fmt.Println("Ohayou Gozaimasu")
case "Thai" : fmt.Println("Arunsawad Krub")
default : fmt.Println("Good morning")
}
}
func main() {
jay := new(Man)
james := new(Man)
john := new(Man)
jy := Greeter(jay)
jm := Greeter(james)
jn := Greeter(john)
jay.language = "Japanese"
james.language = "Thai"
john.language = "British"
jy.Greet() // print "Ohayou Gozaimasu"
jm.Greet() // print "Arunsawad Krub"
jn.Greet() // print "Good morning, dear"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment