Skip to content

Instantly share code, notes, and snippets.

@madflojo
Last active February 13, 2021 19:56
Show Gist options
  • Save madflojo/5bef1d2cf97dc318ff8a3f582960894f to your computer and use it in GitHub Desktop.
Save madflojo/5bef1d2cf97dc318ff8a3f582960894f to your computer and use it in GitHub Desktop.
Interface Article - Speaking Program
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