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?