Golang slices.EqualFunc Function

The slices.EqualFunc function in Golang is part of the slices package, introduced in Go 1.21 as part of the standard library. This function allows you to compare two slices for equality based on a custom comparison function. It is particularly useful when you need to compare slices of different types or when the default equality operator is insufficient.

Table of Contents

  1. Introduction
  2. slices.EqualFunc Function Syntax
  3. Examples
    • Basic Usage
    • Comparing Slices of Structs
    • Handling Complex Equality Conditions
  4. Real-World Use Case Example
  5. Conclusion

Introduction

The slices.EqualFunc function provides a flexible way to compare two slices using a custom equality function. This function allows you to define how elements in the slices should be compared, making it ideal for scenarios where you need to compare complex types, apply specific equality rules, or compare slices of different element types.

slices.EqualFunc Function Syntax

The syntax for the slices.EqualFunc function is as follows:

func EqualFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, eq func(E1, E2) bool) bool

Parameters:

  • s1 S1: The first slice to compare.
  • s2 S2: The second slice to compare.
  • eq func(E1, E2) bool: A custom function that compares two elements, one from each slice. It should return true if the elements are considered equal, and false otherwise.

Returns:

  • bool: true if the slices are considered equal based on the custom comparison function, false otherwise.

Behavior:

  • Custom equality check: The function compares the slices element by element using the provided custom equality function. If all corresponding elements are considered equal by the function, the slices are considered equal.

Examples

Basic Usage

This example demonstrates how to use slices.EqualFunc to compare two slices of integers based on a custom comparison function.

Example

package main

import (
	"fmt"
	"slices"
)

func main() {
	// Define two slices of integers
	slice1 := []int{1, 2, 3}
	slice2 := []int{1, 2, 3}

	// Custom comparison function to check for equality
	eq := func(a, b int) bool {
		return a == b
	}

	// Compare the slices for equality using the custom function
	areEqual := slices.EqualFunc(slice1, slice2, eq)

	// Print the result
	fmt.Println("Slices are equal:", areEqual)
}

Output:

Slices are equal: true

Explanation:

  • The slices.EqualFunc function compares slice1 and slice2 element by element using the custom equality function. Since all corresponding elements are equal, the function returns true.

Comparing Slices of Structs

This example shows how to use slices.EqualFunc to compare slices of structs based on custom equality logic.

Example

package main

import (
	"fmt"
	"slices"
)

type Person struct {
	Name string
	Age  int
}

func main() {
	// Define two slices of Person structs
	people1 := []Person{
		{"Alice", 30},
		{"Bob", 25},
	}
	people2 := []Person{
		{"Alice", 30},
		{"Bob", 25},
	}

	// Custom equality function to compare Person structs by Name and Age
	eq := func(a, b Person) bool {
		return a.Name == b.Name && a.Age == b.Age
	}

	// Compare the slices for equality using the custom function
	areEqual := slices.EqualFunc(people1, people2, eq)

	// Print the result
	fmt.Println("Slices are equal:", areEqual)
}

Output:

Slices are equal: true

Explanation:

  • The slices.EqualFunc function compares people1 and people2 based on both Name and Age fields using the custom equality function. Since all corresponding elements are equal, the function returns true.

Handling Complex Equality Conditions

This example demonstrates how slices.EqualFunc can be used to compare slices with complex equality conditions, such as ignoring certain fields.

Example

package main

import (
	"fmt"
	"slices"
)

type Product struct {
	Name  string
	Price float64
	ID    int
}

func main() {
	// Define two slices of Product structs
	products1 := []Product{
		{"Laptop", 1000, 1},
		{"Phone", 500, 2},
	}
	products2 := []Product{
		{"Laptop", 1000, 3},
		{"Phone", 500, 4},
	}

	// Custom equality function to compare Products by Name and Price only
	eq := func(a, b Product) bool {
		return a.Name == b.Name && a.Price == b.Price
	}

	// Compare the slices for equality using the custom function
	areEqual := slices.EqualFunc(products1, products2, eq)

	// Print the result
	fmt.Println("Slices are equal:", areEqual)
}

Output:

Slices are equal: true

Explanation:

  • The slices.EqualFunc function compares products1 and products2 based on Name and Price only, ignoring the ID field. Since all corresponding elements are considered equal by the custom function, the slices are considered equal.

Real-World Use Case Example: Comparing Configurations with Tolerance

A practical use case for slices.EqualFunc is comparing configurations where small differences, such as timestamps or IDs, should be ignored.

Example: Comparing Configuration Versions

package main

import (
	"fmt"
	"time"
	"slices"
)

type Config struct {
	Version   string
	Timestamp time.Time
}

func main() {
	// Define two slices representing different versions of a configuration
	config1 := []Config{
		{"v1.0", time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)},
		{"v2.0", time.Date(2024, 2, 1, 0, 0, 0, 0, time.UTC)},
	}
	config2 := []Config{
		{"v1.0", time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC)}, // Different timestamp
		{"v2.0", time.Date(2024, 2, 1, 12, 0, 0, 0, time.UTC)}, // Different timestamp
	}

	// Custom equality function to compare Configs by Version only
	eq := func(a, b Config) bool {
		return a.Version == b.Version
	}

	// Compare the configurations for equality using the custom function
	areEqual := slices.EqualFunc(config1, config2, eq)

	// Print the result
	fmt.Println("Configurations are equal:", areEqual)
}

Output:

Configurations are equal: true

Explanation:

  • The slices.EqualFunc function compares the Version field of each configuration, ignoring the Timestamp field. Since the versions are the same, the function returns true.

Conclusion

The slices.EqualFunc function in Go is used for comparing slices using custom logic. It is particularly useful when dealing with complex types or when the criteria for equality go beyond simple comparisons. By using slices.EqualFunc, you can accurately and efficiently compare slices in your Go applications, ensuring that your custom equality conditions are met.

Leave a Comment

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

Scroll to Top