I created the following interface:
type cloneable interface {
clone() cloneable
}
And a person
struct (implements cloneable
):
type person struct {
firstName string
lastName string
age int
}
func (p person) clone() person {
return person{p.firstName, p.lastName, p.age}
}
Now I attempt to clone my person value like so:
p1 := person{"name", "last", 22}
p2 := p1.clone()
fmt.Println(p2 == p1) // PRINTS 'true', why?
The clone method works as intended, but why p2 equals to p1? these are both values, not references, how can they be equal?
Two structs will be equal if first, all their field types are comparable
and all the corresponding field values are equal
.
if your struct has at least one function
or one uncomparable value, then you can not compare two structs