Skip to content

Instantly share code, notes, and snippets.

@santosh
Created November 1, 2022 08:13
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 santosh/1fd57fa8693fcc177392e7204cd40beb to your computer and use it in GitHub Desktop.
Save santosh/1fd57fa8693fcc177392e7204cd40beb to your computer and use it in GitHub Desktop.
Interface as value.
package main
import (
"fmt"
)
type Animal interface {
Speak() string
}
type Dog struct {
}
func (d Dog) Speak() string {
return "Woof!"
}
type Cat struct {
}
func (d *Cat) Speak() string {
return "Meow!"
}
type Llama struct {
}
func (d Llama) Speak() string {
return "??????"
}
type JavaProgrammer struct {
}
func (d JavaProgrammer) Speak() string {
return "Design patterns!"
}
func main() {
animals := []Animal{&Dog{}, &Cat{}, Llama{}, JavaProgrammer{}}
for _, animal := range animals {
fmt.Println(animal.Speak())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment