In Go, you write a *function* (not a method, function) that takes an
interface as a parameter. Boom, polymorphism.
For an example in the std lib (one of hundreds, possibly) look at
fmt.Printf. It's a package-level, free-standing function. Notice its first
parameter - an io.Writer - an interface type. That's how it's done.
Trying to code Go if your only way of thinking is "Java" will be about as
successful as coding Haskell like you code C.
Different languages have different patterns and idioms.
On Sunday, March 18, 2012 12:26:23 PM UTC-6, trialcodr wrote:
Any idea why this code calls A.Foo() even though Bar() is called on a
value of type B?
package main
import (
"fmt"
)
type A struct {
}
func (a *A) Foo() {
fmt.Println("A.Foo()")
}
func (a *A) Bar() {
a.Foo()
}
type B struct {
A
}
func (b *B) Foo() {
fmt.Println("B.Foo()")
}
func main() {
b := B{A: A{}}
b.Bar()
}
--Any idea why this code calls A.Foo() even though Bar() is called on a
value of type B?
package main
import (
"fmt"
)
type A struct {
}
func (a *A) Foo() {
fmt.Println("A.Foo()")
}
func (a *A) Bar() {
a.Foo()
}
type B struct {
A
}
func (b *B) Foo() {
fmt.Println("B.Foo()")
}
func main() {
b := B{A: A{}}
b.Bar()
}
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.