Created
June 26, 2012 18:26
-
-
Save hutch/2997749 to your computer and use it in GitHub Desktop.
related to blog post "A Rubyist has Some Difficulties with 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 Thing interface { | |
Step1AsMethod() | |
Step2AsMethod() | |
} | |
type Base struct { | |
Thing | |
} | |
type Derrived struct { | |
Base | |
} | |
func Step1AsFunction(b Thing) { | |
fmt.Printf("Step1AsFunction (%T) %#v\n", b, b) | |
b.Step2AsMethod() | |
} | |
func (b *Base) Step1AsMethod() { | |
fmt.Printf("Step1AsMethod (%T)\n", b) | |
b.Step2AsMethod() | |
} | |
func (b *Base) Step2AsMethod() { | |
var w Thing = b | |
switch what := w.(type) { | |
default: | |
fmt.Printf("Step2AsMethod (%T/%T) %p %#v\n", b, what, b, b) | |
} | |
} | |
func (d *Derrived) Step2AsMethod() { | |
fmt.Printf("Derrived Step2AsMethod (%T)\n", d) | |
} | |
func main() { | |
fmt.Println("Base...") | |
b := &Base{} | |
b.Step1AsMethod() | |
Step1AsFunction(b) | |
fmt.Println("\nDerrived...") | |
d := &Derrived{} | |
d.Step1AsMethod() | |
Step1AsFunction(d) | |
} |
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
# Implements a simple method, step1, that calls another method, step2. | |
class Base | |
def step1 | |
puts "Base::step1" | |
step2 | |
end | |
def step2 | |
puts "Base::step2" | |
end | |
end | |
# Derrived re-impliments the step2 method, otherwise the same as Base | |
class Derrived < Base | |
def step2 | |
puts "Derrived::step2" | |
end | |
end | |
b = Base.new | |
d = Derrived.new | |
puts("Base...") | |
b.step1 | |
puts("\nDerrived...") | |
d.step1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment