Golang bytes.ContainsRune() function is “used to check whether the rune r contained in the UTF-8-encoded byte slice b.”
Syntax
func ContainsRune(b []byte, r rune) bool
Parameters
- b: The main byte slice in which we have to check the rune value.
- r: The rune value to be checked within the byte slice b.
Return value
It returns true if rune r is within the byte slice b; false, otherwise.
Example 1
package main
import (
"bytes"
"fmt"
)
func main() {
fmt.Println(bytes.ContainsRune([]byte("I like seafood."), 'f'))
fmt.Println(bytes.ContainsRune([]byte("I like seafood."), 'ö'))
}
Output
true
false
Example 2
package main
import (
"bytes"
"fmt"
)
func main() {
b := []byte("hello world")
// Check for the presence of the rune 'l'
if bytes.ContainsRune(b, 'l') {
fmt.Println("The byte slice contains the rune 'l'.")
} else {
fmt.Println("The byte slice doesn't contain the rune 'l'.")
}
// Check for the presence of the rune 'z'
if bytes.ContainsRune(b, 'z') {
fmt.Println("The byte slice contains the rune 'z'.")
} else {
fmt.Println("The byte slice doesn't contain the rune 'z'.")
}
}
Output
The byte slice contains the rune 'l'.
The byte slice doesn't contain the rune 'z'.
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.