Golang bytes.ContainsFunc() Function

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.


func ContainsFunc(b []byte, f func(rune) bool) bool


  1. b: A byte slice 
  2. 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.


package main

import (

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))



That’s it!

Related posts

Golang bytes.Contains()

Golang bytes.Clone()

Golang bytes.Equal()

Golang bytes.Compare()

Golang bytes.containsAny()