Skip to content

Instantly share code, notes, and snippets.

@danielhfrank
Created July 10, 2012 21:34
Show Gist options
  • Save danielhfrank/3086385 to your computer and use it in GitHub Desktop.
Save danielhfrank/3086385 to your computer and use it in GitHub Desktop.
Go Diamond Problem
package main
// Seems like this could be problematic when extending a type in a 3rd party module...
import (
"fmt"
)
type Shredder interface {
Shred()
}
type Biram struct{}
type LeftLane struct{}
func (s *Biram) Shred() {
fmt.Println("Chicken!")
}
func (s *LeftLane) Shred() {
fmt.Println("Shine!")
}
type BillHilly struct {
Biram
LeftLane
}
func ShredRealLoud(s Shredder) {
fmt.Println("!!!!!!!!!!!!!")
s.Shred()
fmt.Println("!!!!!!!!!!!!!")
}
func main() {
bh := &BillHilly{}
bh.Shred() // ./shred.go:35: ambiguous selector bh.Shred
ShredRealLoud(bh) // ./shred.go:36: BillHilly.Shred is ambiguous
// ./shred.go:36: cannot use bh (type BillHilly) as type Shredder in function argument:
// BillHilly does not implement Shredder (missing Shred method)
}
@jordanorelli
Copy link

...eh? that's the expected behavior. bh has no Shred() method; but bh.Biram.Shred() and bh.LeftLane.Shred() exist. You would have to func (b *BillHilly) Shred() { b.Biram.Shred() } to make that work.

@danielhfrank
Copy link
Author

Sure, but it does compile if you comment out either line 25 or 26. I know there's nothing wrong with this behavior, I just found it interesting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment