Menu
Coddy logo textTech

Equals Function

Lesson 17 of 19 in Coddy's Functions and Pointers in Golang course.

In the slices package, there are many built-in functions that we can use when working with slices. One of those functions is the Equals function, which compares two slices and returns a boolean value, true or false.

An example of how we can use this function:

a := []int{0, 42, 8}
b := []int{0, 42, 8}
fmt.Println(slices.Equals(a, b))

Here, we define two slices, a and b. We then call the Equals function, which is defined in the slices package, and we pass our slices. In our example, it will return true because the two slices are equal.

We can also do it this way:

a := []int{0, 42, 8}
fmt.Println(slices.Equals(a, []int{12, 34, 7}))

This will return false because a is not equal to the given slice {12, 34, 7}.

 

 

challenge icon

Challenge

Easy

Let's build our equals function! 😇

Write a function called compare that will take two slices of type int and return a boolean value: true if the slices are equal, and false otherwise.

Refer to the hint if you need more help on how to achieve this 😍.

Try it yourself

// Your Code Here 

All lessons in Functions and Pointers in Golang