Skip to content

Instantly share code, notes, and snippets.

@yuriteixeira
Last active August 29, 2015 14:17
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 yuriteixeira/59925034b37a9c3b8235 to your computer and use it in GitHub Desktop.
Save yuriteixeira/59925034b37a9c3b8235 to your computer and use it in GitHub Desktop.
OOP and Golang: "Inheritance"
package main
import "fmt"
type Foo struct {
what string
howMany int
}
type Bar struct {
what string
howMany int
}
type Doer interface {
Do() string
}
func (f *Foo) Do() string {
return fmt.Sprintf("I'm Foo! I'm doing %s %d time(s)", f.what, f.howMany)
}
func (b *Bar) Do() string {
return fmt.Sprintf("Bar is doing %s %d time(s)", b.what, b.howMany)
}
func GossipGirl(someone Doer) string {
return fmt.Sprintf("I know that %s", someone.Do())
}
type Gang struct {
Foo
Bar
}
func main() {
doer := &Gang{
Foo{"sex", 69},
Bar{"a sandwich", 3},
}
fmt.Println(doer.Do())
// fmt.Println(doer.bar.Do())
fmt.Println(GossipGirl(doer))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment