I seem to be repeating the speak function for example.
Also is there a way to move the initial knowledge of the baby and the human to a default constructor instead of being passed while making a new baby and human?
package main
import (
"fmt"
)
func main() {
h := Human{"good things"}
d := Devil{}
b := Baby{"ga ga"}
b.speak()
h.speak()
d.poisons(&h)
d.poisons(&b)
b.speak()
h.speak()
}
type Human struct {
Knowledge string
}
type Devil struct{}
type Baby struct {
Knowledge string
}
type Knowledgable interface {
changeKnowledge(newKnowledge string)
}
func (d Devil) poisons(creature Knowledgable) {
creature.changeKnowledge(" evil things")
}
func (h Human) speak() {
fmt.Println(h.Knowledge)
}
func (b Baby) speak() {
fmt.Println(b.Knowledge)
}
func (h *Human) changeKnowledge(newKnowledge string) {
h.Knowledge += newKnowledge
}
func (b *Baby) changeKnowledge(newKnowledge string) {
b.Knowledge = newKnowledge
}