×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang bytes.ContainsRune() Function with Examples

Golang | bytes.ContainsRune() Function: Here, we are going to learn about the ContainsRune() function of the bytes package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 22, 2021

bytes.ContainsRune()

The ContainsRune() function is an inbuilt function of the bytes package which is used to check whether the rune r contained in the UTF-8-encoded byte slice b.

It accepts two parameters (b []byte, r rune)) and returns true if rune r is within the byte slice b; false, otherwise.

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

The return type of the bytes.ContainsRune() function is a bool, it returns true if rune r is within the byte slice b; false, otherwise.

Example 1

// Golang program to demonstrate the
// example of bytes.ContainsRune() function

package main

import (
	"bytes"
	"fmt"
)

func main() {
	// Declaring byte slices
	// Assigning  UTF-8-encoded code points
	// of ABCD EF
	var a = []byte{65, 66, 67, 68, 32, 69, 70}

	// Checking whether rune is within []byte
	fmt.Println(bytes.ContainsRune(a, 'A'))
	fmt.Println(bytes.ContainsRune(a, 'E'))
	fmt.Println(bytes.ContainsRune(a, ' '))
	fmt.Println(bytes.ContainsRune(a, 70))
	fmt.Println(bytes.ContainsRune(a, 'X'))
}

Output:

true
true
true
true
false

Example 2

// Golang program to demonstrate the
// example of bytes.ContainsRune() function

package main

import (
	"bytes"
	"fmt"
)

func main() {
	fmt.Println(bytes.ContainsRune(
		[]byte("Hello, world!"), 'H'))
	fmt.Println(bytes.ContainsRune(
		[]byte("Hello, world!"), '!'))
	fmt.Println(bytes.ContainsRune(
		[]byte("Hello, world!"), 'w'))
	fmt.Println(bytes.ContainsRune(
		[]byte("Hello, world!"), 'x'))
}

Output:

true
true
true
false

Golang bytes Package »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.