Go中与Python的”is”运算符相对应的是什么?

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

What is the Go equivalent of Python's "is" operator?

问题

如何在Go中确定两个变量是否引用同一个实例?更具体地说,一个变量的值的变化是否会影响另一个变量的值。

为了进一步明确问题:如何确定两个变量何时满足CPython中的"is"运算符:

a is b
英文:

How do I determine whether 2 variables refer to the same instance in Go? More specifically, that a mutation to the value of one variable would effect the value of the other variable also.

To further refine the question: How would I determine when 2 variables would satisfy the "is" operator per CPython:

a is b

答案1

得分: 4

== 是你想要的,我认为。

如果 a 和 b 的类型是指针,那么 a==b 表示 a 和 b 是指向相同值的指针。

以下程序输出 false

package main

import "fmt"

type test struct {
    a int
}

func main() {

    b := &test{2}
    c := &test{2}
    fmt.Println(c == b)

}

而这个程序输出 true

    b := &test{2}
    c := b
    fmt.Println(c == b)

c==b 是一个足够的条件,当改变 c.a 时,b.a 也会改变。

英文:

EDIT : I'm not sure about what you want. If it's about the equality of variables or the identity of variable values. This answer is for the second one ("2 variables refer to the same instance " of value). If I misunderstood, I'll remove this answer.

== is what you want, I think.

If the type of a and b is pointer, then a==b means that a and b are pointers to the same value.

The following program prints false :

package main

import "fmt"

type test struct {
    a int
}

func main() {

    b := &test{2}
    c := &test{2}
    fmt.Println(c == b)

}

While this prints true :

    b := &test{2}
    c := b
    fmt.Println(c == b)

c==b is a sufficient condition for that changing c.a changes b.a

答案2

得分: 3

在Python中,所有的值都是对象的引用(即指针)。你永远无法获得一个对象本身作为值。is运算符用于比较两个指针值的指针相等性;而==运算符用于比较两个指针值所指向的对象是否相等。

在Go中,情况要复杂一些。Go有指针类型,还有其他非指针类型(布尔型、数值类型、字符串、数组、切片、结构体、函数、接口、映射、通道)。对于非指针类型来说,要求指针相等性是没有意义的(这意味着什么?能达到什么目的?)。

因此,为了达到与Python相同的情况,让我们将所有的值都放在指针后面,这样所有的变量都是指针。在许多Go库中有一个约定,即使用“New”函数创建指针类型;而方法也操作指针类型;因此这与该约定是兼容的。然后(如果ab是指针),在Go中a == b将比较两个指针值的指针相等性;如果它们是可比较的,你可以使用*a == *b来比较底层的值。

Go还有几种非指针引用类型:切片、映射、函数和通道。通道可以使用==来比较它们是否是同一个通道。然而,切片、映射和函数不能进行比较;不过可能可以使用反射来实现。

英文:

In Python, all values are references (i.e. pointers) to objects. You can never get an object itself as a value. The is operator compares two values, which are pointers, for pointer equality; whereas the == operator compares two such pointers, for equality of the objects pointed to.

In Go, it's a little more complicated. Go has pointers, as well as other non-pointer types (boolean, number types, strings, arrays, slices, structs, functions, interfaces, maps, channels). It doesn't make sense to ask for pointer equality for non-pointer types. (What would it mean? What would it accomplish?)

So to have the equivalent situation as Python, let's put all of our values behind pointers, and so all variables are pointers. (There is a convention in many Go libraries of a "New" function that creates a pointer type; and the methods also operate on the pointer type; so this is compatible with that convention.) Then (if a and b are pointers) a == b in Go would compare two such pointers for pointer equality; and you can use *a == *b to compare the underlying values, if they are comparable.

Go also has several non-pointer reference types: slices, maps, functions, and channels. Channels are comparable using == for whether they are the same channel. However, slices, maps, and functions cannot be compared; it may be possible using reflection though.

答案3

得分: 0

在非接口和非函数类型的情况下,可以比较指针是否相等。然而,非指针类型不能共享实例。

英文:

In case of non interface and non function types it is possible to compare pointers for equality. Non pointer types cannot share instances, OTOH.

huangapple
  • 本文由 发表于 2012年5月9日 20:11:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/10515796.html
匿名

发表评论

匿名网友

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

确定