Created
July 10, 2014 01:07
-
-
Save squarism/6496b051533e74ff67aa to your computer and use it in GitHub Desktop.
Playing with this post: http://xampl.com/so/2012/06/27/followup-to-a-rubyist-has-some-difficulties-with-go-limits-to-polymorphism-and-method-dispatch-in-go/
This file contains 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 Doer interface { | |
Do(i int) | |
} | |
type Base struct{} | |
func (b Base) Do(i int) { | |
fmt.Printf("Doer number: %d Base did something.\n", i + 1) | |
} | |
type SubSimple struct { | |
Base | |
} | |
type SubComplex struct { | |
Base | |
} | |
func (s SubComplex) Do(i int) { | |
fmt.Println("Preparing for Base doing.") | |
s.Base.Do(i) | |
fmt.Println("Tear down after Base did.") | |
} | |
func main() { | |
var doers []Doer = []Doer{*new(Base), *new(SubSimple), *new(SubComplex)} | |
for offset, doer := range doers { | |
doer.Do(offset) | |
} | |
} | |
/* | |
Doer number: 1 Base did something. | |
Doer number: 2 Base did something. | |
Preparing for Base doing. | |
Doer number: 3 Base did something. | |
Tear down after Base did. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment