Wednesday, 4 October 2017

go - Checking the equality of two slices



How can I check if two slices are equal?


Answer



You need to loop over each of the elements in the slice and test. Equality for slices is not defined. However, there is a bytes.Equal function if you are comparing values of type []byte.



func testEq(a, b []Type) bool {

// If one is nil, the other must also be nil.

if (a == nil) != (b == nil) {
return false;
}

if len(a) != len(b) {
return false
}

for i := range a {
if a[i] != b[i] {

return false
}
}

return true
}

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...