Created
October 1, 2014 18:08
-
-
Save jochasinga/9f9eee05b648aea04564 to your computer and use it in GitHub Desktop.
Abstracting a method to an interface
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 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