Last active
August 29, 2015 14:17
-
-
Save yuriteixeira/59925034b37a9c3b8235 to your computer and use it in GitHub Desktop.
OOP and Golang: "Inheritance"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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