Last active
February 13, 2021 19:56
-
-
Save madflojo/5bef1d2cf97dc318ff8a3f582960894f to your computer and use it in GitHub Desktop.
Interface Article - Speaking Program
This file contains hidden or 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 Speak interface { | |
SayHello() string | |
} | |
type English struct{} | |
func (e English) SayHello() string { | |
return "Hello" | |
} | |
type Spanish struct{} | |
func (s Spanish) SayHello() string { | |
return "Hola" | |
} | |
func NewVoice(lang string) Speak { | |
switch lang { | |
case "Spanish": | |
return Spanish{} | |
default: | |
return English{} | |
} | |
} | |
func main() { | |
var voice Speak | |
voice = NewVoice("Spanish") | |
fmt.Println(voice.SayHello()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment