Golang bytes.Runes() Function with Examples

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

bytes.Runes()

The Runes() function is an inbuilt function of the bytes package which is used to get a slice of runes (Unicode code points) equivalent to the byte slice (s).

It accepts one parameter (s []byte) and returns a slice of runes (Unicode code points) equivalent to s.

Syntax:

func Runes(s []byte) []rune

Parameter(s):

  • s : The byte slice is to be used to get the slice of runes.

Return Value:

The return type of the bytes.Runes() function is a []rune, it returns a slice of runes (Unicode code points) equivalent to s.

Example 1:

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

package main

import (
	"bytes"
	"fmt"
)

func main() {
	fmt.Println(bytes.Runes(
		[]byte("Hello, world!")))
	fmt.Println(bytes.Runes(
		[]byte("Hello@123!")))
	fmt.Println(bytes.Runes(
		[]byte("ABC DEF XYZ")))
}

Output:

[72 101 108 108 111 44 32 119 111 114 108 100 33]
[72 101 108 108 111 64 49 50 51 33]
[65 66 67 32 68 69 70 32 88 89 90]

Example 2:

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

package main

import (
	"bytes"
	"fmt"
)

func main() {
	str := "Hello, world!"

	result := bytes.Runes([]byte(str))
	for _, r := range result {
		fmt.Printf("%#U\n", r)
	}
}

Output:

U+0048 'H'
U+0065 'e'
U+006C 'l'
U+006C 'l'
U+006F 'o'
U+002C ','
U+0020 ' '
U+0077 'w'
U+006F 'o'
U+0072 'r'
U+006C 'l'
U+0064 'd'
U+0021 '!'

Golang bytes Package »





Comments and Discussions!

Load comments ↻






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