Skip to content

Instantly share code, notes, and snippets.

@reducio
Created March 12, 2019 18:42
Show Gist options
  • Save reducio/a79404ecc2b2413912fa2c2e1da7f81a to your computer and use it in GitHub Desktop.
Save reducio/a79404ecc2b2413912fa2c2e1da7f81a to your computer and use it in GitHub Desktop.
Interface implementation
package main
import (
"fmt"
)
type Entity interface {
Voice() string
}
type Animal struct {
Say string
}
func (a Animal) Voice() string {
return a.Say
}
func main() {
// Impl Dog.
var dog Entity = Animal{"Woof, Woof!"}
fmt.Println("Dog say: ", dog.Voice())
// Impl Cat.
var cat = Animal{"Meow, meow!"}
fmt.Println("Cat say: ", cat.Voice())
// Compare types.
fmt.Println("Compare types: ", dog == cat)
// ==================================
fmt.Println("\n========================\n")
// Init Cat1.
var cat1 Entity
// Impl Cat1.
cat1 = Animal{"Meow, meow!"}
fmt.Println("Cat1 say: ", cat1.Voice())
// Impl Cat2.
var cat2 = Animal{"Meow, meow!"}
fmt.Println("Cat2 say: ", cat2.Voice())
// Compare types.
fmt.Println("Compare cats types: ", cat1 == cat2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment