check if two arrays are equal in Go
Code snippet on how to check if two arrays are equal in Go
package main
import (
"reflect"
"fmt"
)
func main() {
arr1 := []int{1, 2, 3}
arr2 := []int{1, 2, 3}
if reflect.DeepEqual(arr1, arr2) {
fmt.Println("Arrays are equal")
} else {
fmt.Println("Arrays are not equal")
}
}
This code sample will check if two arrays are equal. We start by importing two packages “reflect” and “fmt” from the Go standard library. These packages will enable us to compare arrays logically and print the result respectively. Then, we create two arrays - arr1 and arr2, with the same elements. The code then uses an “if…else” statement that calls “reflect.DeepEqual()” on the arrays. If the arrays contain the same elements in the same order, the result of “reflect.DeepEqual()” will be true, and the code will print “Arrays are equal”. Otherwise, the result of “reflect.DeepEqual()” will be false and the code will print “Arrays are not equal”.