Skip to content

Check Equality of Slices With Order in Golang

If you want to check the equality of two slices and the order is important then this snippet will save your time:

package main

import "fmt"

func main() {
	firstSlice := []int{1, 2, 3, 4, 5}
	secondSlice := []int{1, 2, 3, 4, 5, 6}

	fmt.Println("isEqual", isEqual(firstSlice, secondSlice))
}

func isEqual(first, second []int) bool {
	if len(first) != len(second) {
		return false
	}

	for i, v := range first {
		if v != second[i] {
			return false
		}
	}

	return true
}

Leave a Reply

Your email address will not be published. Required fields are marked *