What is the Go equivalent of the Python equivalent of the "eat" statement?

How to determine if 2 variables belong to the same instance in Go? More specifically, a mutation for the value of one variable will also affect the value of another variable.

To clarify the question: how would I determine when 2 variables will satisfy the "is" operator in CPython:

a is b
+3
source share
3 answers

In Python, all values ​​are references (i.e. pointers) to objects. You can never get an object as a value. The operator iscompares two values, which are pointers, to equal the pointer; whereas the operator ==compares two such pointers to equal the objects that it points to.

Go, . Go , (, , , , , , , , , ). . ( ? ?)

, Python, , . ( Go "", , , .) ( a b ) a == b Go ; *a == *b , .

Go , : , , . == , . , , ; , .

+3

EDIT: , . . ( "2 " ). , .

== - , , .

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

+3

In the case of non-interface and non-functional types, pointers to equality can be compared. Non-pointer types cannot share OTOH instances.

0
source

All Articles