检查 interface{} 的相等性。

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

Checking equality of interface{}

问题

我正在搜索一个 []interface{} 切片,以查找给定的 interface{} 值:

var v interface{}
for i := 0; i < len(A); i++ {
  if (A[i] == v) {
    fmt.Println("Gotcha!")
    break
  }
}

在简单的情况下,类型是 int。但是,如果类型是一些自定义的 struct,我该怎么办?

英文:

I am searching a []interface{} slice for a given interface{} value:

var v interface{}
for i := 0; i &lt; len(A); i++ {
  if (A[i] == v) {
    fmt.Println(&quot;Gotcha!&quot;)
    break
  }
}

In the trivial case the types are int. However what should I do if, for example, the types are some custom struct?

答案1

得分: 49

感谢@CodingPickle的评论,我从《Go编程语言规范》中提供以下内容:

  • interface{}structs方面:
    • 接口值是可比较的。如果两个接口值具有相同的动态类型和相等的动态值,或者两个接口值都为nil,则它们是相等的。
    • 当X类型的值可比较且X实现了T时,非接口类型X的值x和接口类型T的值t是可比较的。如果t的动态类型与X相同且t的动态值等于x,则它们是相等的。
    • 如果结构体的所有字段都是可比较的,则结构体值是可比较的。如果两个结构体值的对应非空字段相等,则它们是相等的。

你也可以尝试这个playground链接:https://play.golang.org/p/bgO1_V87v9k

换句话说,在Go中处理相等性似乎很容易!

英文:

Thanks to @CodingPickle comment, I provide the following from the Go Programming Language Specification

> The equality operators == and != apply to operands that are comparable.

Regarding interface{}s and structs:

  • Interface values are comparable. Two interface values are equal if they have identical dynamic types and equal dynamic values or if both have value nil.
  • A value x of non-interface type X and a value t of interface type T are comparable when values of type X are comparable and X implements T. They are equal if t's dynamic type is identical to X and t's dynamic value is equal to x.
  • Struct values are comparable if all their fields are comparable. Two struct values are equal if their corresponding non-blank fields are equal.

You can also try this playground https://play.golang.org/p/bgO1_V87v9k

In other words, handling equality seems easy in Go!

答案2

得分: 6

我正在更正之前的回答,因为我现在认为它是错误的。当比较两个接口值时,只有当它们是相同类型且为非可比较类型时,运行时才会发生恐慌。如果它们包含不同类型,则结果为false,即使其中一个或两个类型都是非可比较的。

什么是非可比较类型?基本上,它们是切片、映射、函数以及使用它们的任何结构体或数组类型。

英文:

I am correcting my previous answer as I now believe it was wrong. When two interface values are compared the run-time will only panic if they are of the same type and it is non-comparable. If they contain different types then the result is false even if one or both types are non-comparable.

What are non-comparable types? Basically, they are slices, maps, functions and any struct or array type that uses them.

答案3

得分: 6

我有一份关于Go类型属性的摘要。

检查 interface{} 的相等性。

解释:

  • depends - 只有当包含的类型是可比较的时才允许使用。对于接口类型,代码会编译通过,但如果在运行时包含的类型不可比较,运行时会引发恐慌。感谢 @Andrew W. Phillips。
英文:

I have a summary of properties of Go types

检查 interface{} 的相等性。

Explanation

  • depends - it's only allowed if the contained type(s) are comparable.) For Interface types the code will compile but if at runtime the types contained are not comparable then the runtime will panic. Thanks to @Andrew W. Phillips.

答案4

得分: 5

reflect.DeepEqual(x, y any) bool

运行成功。

英文:
reflect.DeepEqual(x, y any) bool

worked.

huangapple
  • 本文由 发表于 2015年12月13日 06:51:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/34245932.html
匿名

发表评论

匿名网友

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

确定