Golang Equivalent of `is` Operator in Python

2019-06-09 07:45发布

问题:

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:

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
}


标签: go identity