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
The error message pretty much says it all:
tmp/sandbox129360726/main.go:42: impossible type assertion:
Cat does not implement IAnimal (Speak method has pointer receiver)
Cat
does not implement IAnimal
, because Speak
(part of the IAnimal
interface) has a pointer receiver, and Cat
is not a pointer.
If you change Cat
to *Cat
, it works:
var same_cat *Cat = zoo.GetAnimal().(*Cat)
The error pretty much says it all, too.
append(zoo.animals, cat)
You're appending cat
to zoo.animals
(evaluating), then throwing away the result, because there's nothing on the left side. You probably want to do this instead:
zoo.animals = append(zoo.animals, cat)
One other side note: When you're assigning to a variable directly, there's no need to specify the type, because Go can determine it for you. Therefore
var same_cat Cat = zoo.GetAnimal().(Cat)
would be better expressed as:
var same_cat = zoo.GetAnimal().(Cat)
or also:
same_cat := zoo.GetAnimal().(Cat)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With