Skip to content

Instantly share code, notes, and snippets.

@charneykaye
Created September 16, 2016 21:22
Show Gist options
  • Save charneykaye/f2c82f4e22181dcf90fb7bd72689039c to your computer and use it in GitHub Desktop.
Save charneykaye/f2c82f4e22181dcf90fb7bd72689039c to your computer and use it in GitHub Desktop.
Golang struct can satisfy three interfaces
package main
import (
"fmt"
)
type Yellow interface {
Red() string
Green() string
}
type Cyan interface {
Green() string
Blue() string
}
type Purple interface {
Red() string
Blue() string
}
type RGB struct {}
func (rgb *RGB) Red() string {
return "RED"
}
func (rgb *RGB) Green() string {
return "GREEN"
}
func (rgb *RGB) Blue() string {
return "BLUE"
}
func Demo(yellow Yellow, cyan Cyan, purple Purple) {
fmt.Println("Yellow is "+yellow.Red()+" and "+yellow.Green())
fmt.Println("Cyan is "+cyan.Blue()+" and "+cyan.Green())
fmt.Println("Purple is "+purple.Red()+" and "+purple.Blue())
}
func main() {
c := &RGB{}
Demo(c, c, c)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment