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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?