golang 为什么比较两个指向结构的变量表现不同?

golang why comparing two variables pointing to struct behaves differently?

我创建了两个相同结构的实例,当我比较两个指向结构实例的变量时,对输出感到困惑。

package main

import "fmt"

type Person struct {
    name string
}

func main() {
   p1 := &Person{name: "guru"}
   p2 := &Person{name: "guru"}
   fmt.Println(p1 == p2) // false, compares by address?
   p3 := Person{name: "guru"}
   p4 := Person{name: "guru"}
   fmt.Println(p3 == p4) // true , why? compares by content?
}

== 运算符是否像重载运算符一样工作?

p1 == p2是指针比较,比较指针值(内存地址)。由于您使用了 2 个复合文字(并获取了它们的地址),它们将指向 2 个不同的变量,因此地址将不同(因为 Person 的大小不为零)。 Spec: Composite literals:

Taking the address of a composite literal generates a pointer to a unique variable initialized with the literal's value.

p3 == p4 比较结构值,它比较它们 field-by-field,并且由于匹配字段具有相等的值,比较将导致 true.

比较规则在Spec: Comparison operators:

The equality operators == and != apply to operands that are comparable. The ordering operators <, <=, >, and >= apply to operands that are ordered. These terms and the result of the comparisons are defined as follows:

  • [...]
  • Pointer values are comparable. Two pointer values are equal if they point to the same variable or if both have value nil. Pointers to distinct zero-size variables may or may not be equal.
  • [...]
  • Struct values are comparable if all their fields are comparable. Two struct values are equal if their corresponding non-blank fields are equal.

两个指针值只有在内存中指向相同的值或者在Golang中为nil时才相等。 您创建了两个结构实例,因此它们具有不同的地址

   p1 := &Person{name: "guru"}
   p2 := &Person{name: "guru"}

你的第一个例子

p1 := &Person{name: "guru"}
p2 := &Person{name: "guru"}

fmt.Println(p1 == p2) // false, compares by address?

比较两个指针是否相等。由于它们各自寻址不同的内存地址,所以不比较相等。

你的第二个例子,

 p3 := Person{name: "guru"}
 p4 := Person{name: "guru"}

fmt.Println(p3 == p4) // true , why? compares by content?

比较两个结构并按值进行比较,因此它们比较相等。

如果您在比较之前取消引用 指针,您会发现它们比较相等。例如,给定

p1 := &Person{ name: "guru" }
p2 := &Person{ name: "guru" }
p3 :=  Person{ name: "guru" }
p4 :=  Person{ name: "guru" }

以下所有比较相等:

  • *p1 == *p2
  • *p1 == p3
  • p3 == *p2
  • p3 == p4