Golang bytes.ToValidUTF8() Function with Examples

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

bytes.ToValidUTF8()

The ToValidUTF8() function is an inbuilt function of the bytes package which is used to get a copy of the byte slice (s – treated as UTF-8-encoded bytes) with each run of bytes representing invalid UTF-8 replaced with the bytes in replacement, which may be empty.

It accepts two parameters (s, replacement []byte) and returns a copy with each run of bytes representing invalid UTF-8 replaced with the bytes in replacement.

Syntax:

func ToValidUTF8(s, replacement []byte) []byte

Parameter(s):

  • s : The byte slice to be used to get of copy of the byte slice where Invalid UTF-8 bytes are replaced with the byte slice (replacement).
  • replacement : The byte slice to be replaced with an invalid UTF-8 rune value.

Return Value:

The return type of the bytes.ToValidUTF8() function is a []byte, it returns a copy with each run of bytes representing invalid UTF-8 replaced with the bytes in replacement.

Example 1:

// Golang program to demonstrate the
// example of bytes.ToValidUTF8() Function

package main

import (
	"bytes"
	"fmt"
)

func main() {
	fmt.Printf("%s\n", bytes.ToValidUTF8(
		[]byte("Hello, world!"), []byte("")))
	fmt.Printf("%s\n", bytes.ToValidUTF8(
		[]byte("Hello, world!"), []byte("OKAY")))

	// Here, \xC9 is an invalid UTF-8 byte
	// and, it will be replaced with OKAY
	fmt.Printf("%s\n", bytes.ToValidUTF8(
		[]byte("Hello,\xC9world!"), []byte("OKAY")))

	// Here, \x41 is a valid UTF-8 for character 'A'
	// but, \xC9 is invalid which will be ignored
	fmt.Printf("%s\n", bytes.ToValidUTF8(
		[]byte("Hello,\xC9\x41 world!"), []byte("")))
}

Output:

Hello, world!
Hello, world!
Hello,OKAYworld!
Hello,A world!

Example 2:

// Golang program to demonstrate the
// example of bytes.ToValidUTF8() Function

package main

import (
	"bytes"
	"fmt"
)

func main() {
	// replacing with empty string i.e.,
	// ignoring the invalid UTF-8 byte sequences
	fmt.Printf("%s\n", bytes.ToValidUTF8(
		[]byte("A\xfcB\xa1C\xa1D\xa1E\xa1F\xa1"), []byte("")))

	// replacing with space
	fmt.Printf("%s\n", bytes.ToValidUTF8(
		[]byte("A\xfcB\xa1C\xa1D\xa1E\xa1F\xa1"), []byte(" ")))

	// replacing with "###"
	fmt.Printf("%s\n", bytes.ToValidUTF8(
		[]byte("A\xfcB\xa1C\xa1D\xa1E\xa1F\xa1"), []byte("###")))

	// there is no invalid UTF-8 byte sequence
	fmt.Printf("%s\n", bytes.ToValidUTF8(
		[]byte("Hello, world!"), []byte("")))
}

Output:

ABCDEF
A B C D E F 
A###B###C###D###E###F###
Hello, world!

Golang bytes Package »




Comments and Discussions!

Load comments ↻





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