使用[]string字段时出现不可比较类型错误(Go语言)

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

Uncomparable type error when []string field used (Go lang)

问题

我有一个特定的Go语言结构体对象,我正在与之交互,并且我期望它与自身相等。我将该函数传递给一个接受interface{}输入/输出的函数,该函数简单地返回它。

type Animal struct {
    name string
    food interface{}
}

type YummyFood struct {
    calories int
}

func echo_back(input interface{}) interface{} {
    return input
}

func main() {
    var tiger_food = YummyFood{calories: 1000}
    var tiger = Animal{name: "Larry", food: tiger_food}
    output_tiger := echo_back(tiger)

    fmt.Printf("%T, %+v\n", tiger, tiger)
    fmt.Printf("%T, %+v\n", output_tiger, output_tiger)
    fmt.Println(tiger == output_tiger)
    fmt.Println(tiger == output_tiger.(Animal))
}

运行这段代码,你会发现tiger和output_tiger看起来是相同的,并且评估为相等。很好,这是我所期望的。现在,尝试使用以下YummyFood的定义:

type YummyFood struct {
    calories int
    ingredients []string
}

突然间,echo_back的输出与输入不再相等,即使进行了类型断言。我得到了"panic: runtime error: comparing uncomparable type YummyFood"的错误。

问题是为什么添加[]string类型会导致输入和输出不可比较?我该如何修改output_tiger才能得到与输入相同的结果(即我期望它与"tiger"相同)?如何反射output_tiger使其等于tiger?

英文:

I have a particular Go lang struct object that I'm interacting with and I expect it to be equal to itself. I'm passing the function to a function that simply returns it but that does it by accepting interface{} inputs/outputs

type Animal struct {
    name string
    food interface{}
}

type YummyFood {
    calories int
}

func echo_back(input interface{}) interface{} {
    return input
}

func main() {
    var tiger_food = Food{calories: 1000}
    var tiger = Animal{name: "Larry", food: tiger_food}
    output_tiger := echo_back(tiger)

    fmt.Printf("%T, %+v\n", tiger, tiger)
    fmt.Printf("%T, %+v\n", output_tiger, output_tiger)
    fmt.Println(tiger == output_tiger)
    fmt.Println(tiger == output_tiger.(Animal))

}

Running this, you see that the tiger and output_tiger appear to be the same and evaluate to be equal. Great. That's what I would expect. NOW, try using this definition for YummyFood

type YummyFood {
    calories int
    ingredients []string
}

All of a sudden, the output from echo_back does NOT evaluate to be the same as the input, even with the type assertion. I get 'panic: runtime error: comparing uncomparable type YummyFood'

The question is why does the addition of the []string type cause the input and output to be uncomparable? How can I modify output_tiger to get back the same thing I put in (i.e I expect it to be the same as 'tiger'). How do I reflect output_tiger to make it equal to tiger?

答案1

得分: 15

它可能不会很快,但你可以使用reflect.DeepEqual()来比较切片(或者只是YummyFood)。它会遍历切片并比较所有元素。你可能可以编写一个更高效的函数来完成这个任务,但如果现有的函数足够快,你就可以节省一些工作量,也许还能避免一些错误。 使用[]string字段时出现不可比较类型错误(Go语言)

英文:

It won't be fast, but you can use reflect.DeepEqual() to compare the slices (or just YummyFood). It does the work to loop through it to compare all the elements. You could probably make a more efficient function to do it, but if the one that's there is fast enough you'll save yourself some work and maybe some bugs. 使用[]string字段时出现不可比较类型错误(Go语言)

答案2

得分: 14

切片([]string)没有定义相等性。包含有定义相等性的成员的数组和结构体将具有定义的相等性。

> 需要注意的是,对于切片,相等性仍然未定义,因为一般情况下计算是不可行的。还需要注意的是,对于结构体和数组,有序比较运算符(< <= > >=)仍然未定义。

来源:http://golang.org/doc/go1.html#equality

英文:

Slices ([]string) has no equality defined. Arrays and structs that contains members that has equality defined will have equality defined.

> Note that equality is still undefined for slices, for which the calculation is in general infeasible. Also note that the ordered comparison operators (< <= > >=) are still undefined for structs and arrays.

From http://golang.org/doc/go1.html#equality

huangapple
  • 本文由 发表于 2013年4月9日 12:22:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/15893250.html
匿名

发表评论

匿名网友

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

确定