Hi Gophers,
I've got myself confused with Function Types.
There are two exactly equivalent interfaces, and it seems a function which takes the first isn't able to be used to satisfy the function type of the second.
In this trivial example, I'd just use the same interface, but in the real example the interfaces are split into two packages.
Possible solutions:
- One 'master' package the other depends on
- One 'interfaces' package
- Functions which convert between the two
So the questions I have:
- Is there an 'easy way' - like a missing 'type assertion' or other language feature I am missing which would make this code work or?
- Is there a way to do something similar to this so those interfaces can exist in two packages which don't depend on each other?
- And... would this make sense as a language feature?
type Eggplant interface {
type Aubergine interface {
ep = au // Just checking, they are equivalent
PeelEggplant(au) // Can send an Aubergine to a Eggplant function, no probs
var testFunc func(Eggplant)
testFunc = func(ep Eggplant) {} // Works, obviously
testFunc = func(ep Aubergine) {} // Gives the below error
var peeler EggplantPeeler
peeler = &AuberginePeeler{} // And the second error
func PeelEggplant(ep Eggplant) {
type EggplantPeeler interface {
type AuberginePeeler struct{}
func (pw *AuberginePeeler) Peel(a Aubergine) {
Gives two compiler errors: (not to mention the nil pointers if it were to run :-))
src/ift/main/interfaceThing.go:26: cannot use func literal (type func(Aubergine)) as type func(Eggplant) in assignment
src/ift/main/interfaceThing.go:29: cannot use AuberginePeeler literal (type *AuberginePeeler) as type EggplantPeeler in assignment:
*AuberginePeeler does not implement EggplantPeeler (wrong type for Peel method)
have Peel(Aubergine)
want Peel(Eggplant)
Thanks!