I'm working with a library that implements a type which accepts handlers and calls them for you when it should. I'd like to create a supertype that embeds the type and has properties beyond what the embedded type does. And I'd like to be able to use these properties in a handler.
Using my type as an argument fails the type checking, using the base type in handler decl works, but then I cant access the new fields. I am new to go, & would love to know how to do this, or what to suggest in changing the library to enable (Interfaces instead of the handler decl?)...
Contrived example:
package main
type Animal struct {
Color string
feeders map[string]feeder
}
type feeder func(*Animal, string) string
func (a *Animal) addFeeder(name string, fn feeder) {
a.feeders[name] = fn
}
type mamal struct {
Animal
hair string
}
func feedHuman(m *mamal, food string) string {
return "you got " + m.Color + " " + m.hair + " hair in your " + food
}
func main() {
a := mamal{Animal{Color: "red"}, "bushy"}
a.addFeeder("man", feedHuman)
// fails to compile feedHuman needs to take *Animal but then cant access hair"
}