将接口类型转换为实际类型时,不可能进行类型断言。

huangapple go评论91阅读模式
英文:

Impossible type assertions with casting from interface type to the actual type

问题

我正在为你翻译以下内容:

我遇到了两个错误:

a. 不可能的类型断言。我们能将接口类型转换为实际类型对象吗?

b. 不确定"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{}

// 添加一只猫
var cat IAnimal = &Cat{}
append(zoo.animals, cat) // 错误1:append(zoo.animals, cat) 被评估但未使用

// 获取猫
var same_cat Cat = zoo.GetAnimal().(Cat) // 错误2:不可能的类型断言

fmt.Println(same_cat)

你可以在Go Playground上运行代码。

英文:

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

答案1

得分: 30

  1. 错误信息已经很清楚了:

     tmp/sandbox129360726/main.go:42: 不可能的类型断言:
         Cat没有实现IAnimal(Speak方法具有指针接收器)
    

Cat没有实现IAnimal,因为SpeakIAnimal接口的一部分)具有指针接收器,而Cat不是指针类型。

如果将Cat改为*Cat,就可以正常工作:

    var same_cat *Cat = zoo.GetAnimal().(*Cat)
  1. 错误信息也已经很清楚了。

      append(zoo.animals, cat)
    

你正在将cat添加到zoo.animals(求值),然后丢弃结果,因为左侧没有任何内容。你可能想要这样做:

    zoo.animals = append(zoo.animals, cat)

另外需要注意的是:当你直接赋值给一个变量时,不需要指定类型,因为Go可以为你确定类型。因此,

var same_cat Cat = zoo.GetAnimal().(Cat)

可以更好地表达为:

var same_cat = zoo.GetAnimal().(Cat)

或者也可以写成:

same_cat := zoo.GetAnimal().(Cat)
英文:
  1. 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)
  1. 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)

huangapple
  • 本文由 发表于 2017年3月14日 05:35:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/42773848.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定