My question is slightly different from this question that asks about how to check equality of Go slices.
Like this article suggests, a Go slice is a value consisting of three things: a pointer to the array, the length of the segment, and its capacity (the maximum length of the segment). Is it then possible to (cheaply) check if two such slices are equal because they point to the same underlying array and have the same values for length and capacity (preferably without traversing the two slices checking for equality of individual elements)? It appears that the ==
operator is not defined on slices.
The question came while I was implementing a bit vector (IntSet
) that internally uses a []uint64
to represent the elements and I stumbled upon implementing a method func (*IntSet) Equals(that *IntSet) bool
which could be called like s.Equals(s)
.
(It appears that I could optimize for that case as shown below, but the question remains:
func (this *IntSet) Equals(that *IntSet) bool {
if this == that { // use equality of pointers!
return true
}
// omitted for brevity
}