Skip to content

Instantly share code, notes, and snippets.

@hutch
Created June 26, 2012 18:26
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 hutch/2997749 to your computer and use it in GitHub Desktop.
Save hutch/2997749 to your computer and use it in GitHub Desktop.
related to blog post "A Rubyist has Some Difficulties with Go"
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)
}
# 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