Skip to content

Instantly share code, notes, and snippets.

@squarism
Created July 10, 2014 01:07
Show Gist options
  • Save squarism/6496b051533e74ff67aa to your computer and use it in GitHub Desktop.
Save squarism/6496b051533e74ff67aa to your computer and use it in GitHub Desktop.
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