dongmangwei3822 2016-02-22 04:57
浏览 52
已采纳

Python中is运算符的Golang等效项

I'm a Python developer who is learning Go and am writing a simple singly linked list implementation as an exercise. I had done this a few years ago in Python and am duplicating now in Go.

One of the methods in the assignment (I did this initially in school) was remove(node): remove a given node form the list. In Python I did this using the is operator. Something like this:

def remove(self, node):
     element = self.head
     prev = None
     while element:
         if element is node:
             remove node form list...
         prev = element
         element = element.next

In Python the is operator checks identity. So for example

>>> class Foo(object):
...     def __init__(self, x):
...         self.x = x
... 
>>> foo = Foo(5)
>>> bar = Foo(5)
>>> baz = foo
>>> foo is baz
True
>>> foo is bar
False

Even though the values on the instances foo and bar are the same they are not the same object, as we see here:

>>> id(foo)
139725093837712
>>> id(bar)
139725093837904

However foo and baz are the same object:

>>> id(foo)
139725093837712
>>> id(baz)
139725093837712

How would I go about doing the same thing in Go? The equality operator, ==, just checks that the values are the same:

package main

import "fmt"

type Test struct {
    x int
}

func main() {
  a := Test{5}
  b := Test{5}
  c := Test{6}

  fmt.Println("a == b", a == b)
  fmt.Println("a == c ", a == c)
  fmt.Println("b == c ", a == c)
}

Which outputs:

a == b true
a == c  false
b == c  false

Playground link

a and b have the same value but are not the same object. Is there a way to check for identity in Go similar to Python? Or is is there a package available for it or some way to roll an identity checking function myself?

  • 写回答

1条回答 默认 最新

  • doumeilmikv7099 2016-02-22 05:27
    关注

    What you're talking about would require the use of pointers in Go. In your Python code, foo, bar, and baz contain references to objects, so you can talk about whether two of them refer to the same underlying object. In your Go code, a, b, and c are variables of type Test. If you declared them as pointers to Test (*Test), you would see something different. Try this:

    package main
    
    import "fmt"
    
    type Test struct {
        x int
    }
    
    func main() {
        // a, b, and c are pointers to type Test
        a := &Test{5}
        b := &Test{5}
        c := a
    
        fmt.Println("a == b", a == b)     // a and b point to different objects
        fmt.Println("a == c", a == c)     // a and c point to the same object
        fmt.Println("*a == *b", *a == *b) // The values of the objects pointed to by a and b are the same
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面