【发布时间】:2017-08-02 04:30:00
【问题描述】:
在下面的示例中遇到类型断言错误。
错误:
49: 无法将 z(IZoo 类型)转换为 Zoo 类型:需要类型断言
49: 不能分配给 Zoo(z).animals
type IAnimal interface {}
type IZoo interface {}
type Zoo struct {
animals map[string]IAnimal
}
func NewZoo() *Zoo {
var z IZoo = &Zoo{}
Zoo(z).animals = map[string]IAnimal{} // cannot convert z (type IZoo) to type Zoo: need type assertion
return z // cannot use z (type IZoo) as type *Zoo in return argument: need type assertion
}
【问题讨论】:
标签: go type-assertion