Skip to content

Instantly share code, notes, and snippets.

@lovemapa
Created February 17, 2021 15:31
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 lovemapa/d7edb9ac93f81368fdf797887ab5dc86 to your computer and use it in GitHub Desktop.
Save lovemapa/d7edb9ac93f81368fdf797887ab5dc86 to your computer and use it in GitHub Desktop.
Go interfaces
package interface
import "fmt"
type Animal interface {
makeSound() string
}
type Goat struct{}
type Dog struct{}
func main() {
g := Goat{}
d := Dog{}
printVoice(g)
printVoice(d)
}
func printVoice(a Animal) {
fmt.Println(a.makeSound())
}
func (Goat) makeSound() string {
return "Bleats"
}
func (Dog) makeSound() string {
return "Barks"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment