Menu Close

Go – bytes.ContainsRune() function in go

bytes.ContainsRune() function is used to check whether rune is present in the another UTF-8-encoded byte slice in go.

bytes.ContainsRune() function prototype:

func ContainsRune(b []byte, r rune) bool

Input parameters:
b: byte slice
r: rune type value to be searched in b

bytes.ContainsRune() function return value:

bytes.ContainsRune() function returns true If rune is present within another byte slice else false.

Explanation:

1)
b := []byte("I like vegetable.")
rune_val := 'b'
Output: true

Here, rune 'b' in rune_val is present in b i.e "I like vegetable."

2)
b := []byte("I like vegetable.")
rune_val := 'ö'
Output: false

Here, 'ö' in rune_val is not present in b i.e "I like vegetable."

3) b := []byte("去是伟大的!")
rune_val := '大'
Output: true

Here, rune '大' in rune_val is present in b i.e "去是伟大的!"

Example:

package main

import (
	"bytes"
	"fmt"
)

func main() {
    b := []byte("I like vegetable.")
    rune_val := 'b'
	fmt.Println(bytes.ContainsRune(b, rune_val))
	
	b = []byte("I like vegetable.")
	rune_val = 'ö'
	fmt.Println(bytes.ContainsRune(b, rune_val))
	
	b = []byte("去是伟大的!")
	rune_val = '大'
	fmt.Println(bytes.ContainsRune(b, rune_val))
	
}

Output:

true
false
true

To learn more about golang, Please refer given below link:

References:

https://golang.org/doc/

Posted in bytes, golang, packages

Leave a Reply

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