I'm getting two errors,
a. Impossible Type assertion. Can we cast from interface type to the actual type object
b. not sure what's the meaning of evaluated but not used
type IAnimal interface {
Speak()
}
type Cat struct{}
func (c *Cat) Speak() {
fmt.Println("meow")
}
type IZoo interface {
GetAnimal() IAnimal
}
type Zoo struct {
animals []IAnimal
}
func (z *Zoo) GetAnimal() IAnimal {
return z.animals[0]
}
Testing
var zoo Zoo = Zoo{}
// add a cat
var cat IAnimal = &Cat{}
append(zoo.animals, cat) // error 1: append(zoo.animals, cat) evaluated but not used
// get the cat
var same_cat Cat = zoo.GetAnimal().(Cat) // error 2: impossible type assertions
fmt.Println(same_cat)
go Playground