Created
December 24, 2016 11:19
-
-
Save khajavi/03823f934c5291ae7cd1a83479eedbc6 to your computer and use it in GitHub Desktop.
Duck Typing Example in Golang
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 Speaker interface { | |
Say(string) | |
} | |
type Person struct { | |
name string | |
} | |
func (p *Person) Say(message string) { | |
fmt.Println(p.name + ":" , message) | |
} | |
func SpeakAlphabet(via Speaker) { | |
via.Say("abcdefgh") | |
} | |
func main() { | |
mat := new(Person) | |
mat.name = "Mat" | |
SpeakAlphabet(mat) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment