【发布时间】:2017-03-13 21:35:48
【问题描述】:
我遇到了两个错误,
一个。不可能的类型断言。我们可以从接口类型转换为实际的类型对象吗
b.不知道评估但未使用是什么意思
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]
}
测试
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