为什么克隆的结构值仍然等于原始结构

Why cloned struct value still equals to the original struct

我创建了以下界面:

type cloneable interface {
    clone() cloneable
}

和一个 person 结构(实现 cloneable):

type person struct {
    firstName string
    lastName  string
    age       int
}

func (p person) clone() person {
    return person{p.firstName, p.lastName, p.age}
}

现在我尝试像这样克隆我的个人价值:

p1 := person{"name", "last", 22}
p2 := p1.clone()

fmt.Println(p2 == p1) // PRINTS 'true', why?

克隆方法按预期工作,但为什么 p2 等于 p1?这两个都是值,不是引用,怎么能相等呢?

两个结构相等,如果首先,它们的所有字段类型都是comparable并且所有对应的字段值都是equal

如果你的结构至少有一个function或一个不可比较的值,那么你不能比较两个结构

来自The Go Programming Language Specification

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

The Go Programming Language Specification: Comparison operators