Golang slices.ContainsFunc Function

The slices.ContainsFunc 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 determine if any element in a slice satisfies a given condition defined by a custom function. It is particularly useful when you need to check for the presence of an element that meets specific criteria, rather than simply checking for a direct match.

Table of Contents

  1. Introduction
  2. slices.ContainsFunc Function Syntax
  3. Examples
    • Basic Usage
    • Checking for Complex Conditions
    • Using slices.ContainsFunc with Structs
  4. Real-World Use Case Example
  5. Conclusion

Introduction

The slices.ContainsFunc function provides a flexible way to check if any element in a slice satisfies a custom condition. Unlike slices.Contains, which checks for exact matches, slices.ContainsFunc allows you to define a custom condition using a function. This makes it particularly useful when working with slices of complex types or when the criteria for "containment" go beyond simple equality.

slices.ContainsFunc Function Syntax

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

func ContainsFunc[S ~[]E, E any](s S, f func(E) bool) bool

Parameters:

  • s S: The slice to check.
  • f func(E) bool: A custom function that defines the condition to check for each element in the slice. It should return true if the condition is met, false otherwise.

Returns:

  • bool: true if any element in the slice satisfies the condition defined by f, false otherwise.

Behavior:

  • Custom condition checking: The function iterates over the slice and applies the custom function to each element. If the function returns true for any element, slices.ContainsFunc returns true.

Examples

Basic Usage

This example demonstrates how to use slices.ContainsFunc to check if a slice of integers contains any even number.

Example

package main

import (
	"fmt"
	"slices"
)

func main() {
	// Define a slice of integers
	numbers := []int{1, 3, 5, 7, 9}

	// Define a custom function to check for even numbers
	isEven := func(n int) bool {
		return n%2 == 0
	}

	// Use slices.ContainsFunc to check if any even number is in the slice
	hasEven := slices.ContainsFunc(numbers, isEven)

	// Print the result
	fmt.Println("Contains even number:", hasEven)
}

Output:

Contains even number: false

Explanation:

  • The slices.ContainsFunc function checks each element in the numbers slice using the isEven function. Since there are no even numbers, it returns false.

Checking for Complex Conditions

This example shows how to use slices.ContainsFunc to check if a slice of strings contains any string longer than 5 characters.

Example

package main

import (
	"fmt"
	"slices"
)

func main() {
	// Define a slice of strings
	words := []string{"apple", "banana", "cherry", "date"}

	// Define a custom function to check for strings longer than 5 characters
	isLong := func(s string) bool {
		return len(s) > 5
	}

	// Use slices.ContainsFunc to check if any string is longer than 5 characters
	hasLongWord := slices.ContainsFunc(words, isLong)

	// Print the result
	fmt.Println("Contains word longer than 5 characters:", hasLongWord)
}

Output:

Contains word longer than 5 characters: true

Explanation:

  • The slices.ContainsFunc function checks each string in the words slice using the isLong function. Since "banana" and "cherry" are longer than 5 characters, it returns true.

Using slices.ContainsFunc with Structs

This example demonstrates how slices.ContainsFunc can be used to check for a specific condition in a slice of structs.

Example

package main

import (
	"fmt"
	"slices"
)

type Person struct {
	Name string
	Age  int
}

func main() {
	// Define a slice of Person structs
	people := []Person{
		{"Alice", 30},
		{"Bob", 25},
		{"Charlie", 35},
	}

	// Define a custom function to check if any person is over 30 years old
	isOver30 := func(p Person) bool {
		return p.Age > 30
	}

	// Use slices.ContainsFunc to check if any person is over 30
	hasOver30 := slices.ContainsFunc(people, isOver30)

	// Print the result
	fmt.Println("Contains person over 30:", hasOver30)
}

Output:

Contains person over 30: true

Explanation:

  • The slices.ContainsFunc function checks each Person struct in the people slice using the isOver30 function. Since "Charlie" is over 30, it returns true.

Real-World Use Case Example: Validating User Input

A practical use case for slices.ContainsFunc is validating user input, such as checking if a slice of strings contains any prohibited words.

Example: Checking for Prohibited Words

package main

import (
	"fmt"
	"slices"
)

func main() {
	// Define a slice of user input strings
	inputs := []string{"hello", "world", "spam", "goodbye"}

	// Define a slice of prohibited words
	prohibited := []string{"spam", "scam"}

	// Define a custom function to check for prohibited words
	isProhibited := func(s string) bool {
		return slices.Contains(prohibited, s)
	}

	// Use slices.ContainsFunc to check if any input contains a prohibited word
	hasProhibited := slices.ContainsFunc(inputs, isProhibited)

	// Print the result
	fmt.Println("Contains prohibited word:", hasProhibited)
}

Output:

Contains prohibited word: true

Explanation:

  • The slices.ContainsFunc function checks each string in the inputs slice against the list of prohibited words. Since "spam" is in the list, it returns true.

Conclusion

The slices.ContainsFunc function in Go is used for checking if any element in a slice satisfies a custom condition. It is particularly useful when dealing with complex types or when the criteria for "containment" require custom logic. By using slices.ContainsFunc, you can efficiently validate and filter data in your Go applications, ensuring that your conditions are met.

Leave a Comment

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

Scroll to Top