Series

Here are all the posts in this series about the slices package.

Introduction

In the last post of this series I discussed the Clip, Clone, and Compact APIs from the slices package. In this post, I will share how the Compare and CompareFunc APIs work.

Compare

The Compare API can be used to compare two slices and determine if the slices are equal or which one is shorter. Shorter meaning the slice with the lesser of the two values at the same index position. Less can be obvious like with integers, or needs to be custom defined like with structs.

The code that will be reviewed for this example can be found here.

https://github.com/ardanlabs/gotraining/blob/master/topics/go/packages/slices/example5/example5.go

To start, a map is declared that associates the values of -1, 0, and 1 to strings that describe the result of calling the Compare API.

Listing 1

08 var result = map[int]string{
09     -1: "First slice is shorter",
10     0:  "Both slices are equal",
11     1:  "Second slice is shorter",
12 }

Listing 1 shows the mapping between the integer result and what that result means. This will be used for output from the program.

Next, a few slices with values need to be constructed.

Listing 2

14 func main() {
15     list1 := []int{1, 2, 3, 4, 5}
16     list2 := []int{1, 2, 6, 4, 5}
17     list3 := []int{1, 2, 3, 4, 5}
18
19     fmt.Println("Slice1", list1)
20     fmt.Println("Slice2", list2)
21     fmt.Println("Slice3", list3)

Listing 2 shows the construction and printing of 3 slices. Slice 1 and 3 are identical and slice 2 is larger at index 2 compared to slice 1 and 3.

Listing 3

23     fmt.Printf("list1 vs list2: Compare(%s), Func(%s)\n",
24         result[slices.Compare(list1, list2)],
25         result[slices.CompareFunc(list1, list2, compare)],
26     )

Listing 3 shows the calls to Compare and CompareFunc and compounds a few things together. Inside a call to fmt.Printf, a call to the Compare and CompareFunc APIs are made inside a map lookup for the result map. The list1 and list2 slices are compared. For the CompareFunc API call, a custom compare function is provided.

Listing 4

40 func compare(a int, b int) int {
41     if a < b {
42         return -1
43     }
44
45     if a > b {
46         return 1
47     }
48
49     return 0
50 }

Listing 4 shows the custom compare function. This function receives two values from the slices being compared. A value of 0 is returned when the two values match, -1 is returned when a is less than b, and 1 is returned when a is greater than b. Thanks to generics, the compiler will know if the custom function’s parameters are of the same type as the slices being passed into the CompareFunc API.

Listing 5

list1 vs list2:
Compare(First slice is shorter)
Func(First slice is shorter)

Listing 5 shows the output for the code in listing 3. In this case the Compare APIs report that list1 is a shorter slice than list2. That is correct since list2 has the value of 6 at index 2 and list1 has the value of 3 at the same index position.

Listing 6

28     fmt.Printf("list1 vs list3: Compare(%s), Func(%s)\n",
29         result[slices.Compare(list1, list3)],
30         result[slices.CompareFunc(list1, list3, compare)],
31     )

Listing 6 shows the calls to Compare and CompareFunc again but this time using the two lists that are identical.

Listing 7

list1 vs list3:
Compare(Both slices are equal)
Func(Both slices are equal)

Listing 7 shows the output for the code in listing 6. In this case the Compare APIs report that list1 is equal to list3.

Conclusion

You can see how cool this new slices package is by providing the Compare and CompareFunc APIs. In the next post, we will explore the Contains, and Equal APIs.

If you want to cheat and see all the examples I have prepared for future posts, check out the Go training repo.

https://github.com/ardanlabs/gotraining/tree/master/topics/go/packages/slices

Trusted by Top Technology Companies

We've built our reputation as educators and bring that mentality to every project. When you partner with us, your team will learn best practices and grow along the way.

30,000+

Engineers Trained

1,000+

Companies Worldwide

14+

Years in Business