The bytes.ContainsFunc() function is added in Go 1.20 that returns true if any Unicode code points in the byte slice b satisfy the predicate f. It is similar to using bytes.IndexFunc() but more convenient.
Syntax
func ContainsFunc(b []byte, f func(rune) bool) bool
Parameters
- b: A byte slice
- f: The function parameter must accept a rune as an input and return a bool as an output.
Return value
The ContainsFunc function returns true if any of the runes in the byte slice satisfy the function parameter; false, otherwise.
Example
package main
import (
"bytes"
"fmt"
"unicode"
)
func main() {
// Create a byte slice
b := []byte("Hello, 世界!")
// Check if it contains any uppercase letters
fmt.Println(bytes.ContainsFunc(b, unicode.IsUpper))
// Check if it contains any punctuation marks
fmt.Println(bytes.ContainsFunc(b, unicode.IsPunct))
// Check if it contains any digits
fmt.Println(bytes.ContainsFunc(b, unicode.IsDigit))
}
Output
true
true
false
That’s it!
Related posts

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.