How to check if an array of bytes is a valid UTF-8 string in Go

1 Answer

0 votes
package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {
	{
		str := []byte("Hello, 世界")
		b := utf8.Valid(str)
		fmt.Println(b)
	}
	
	{
		str := []byte{0xa3, 0xed, 0xf}
		b := utf8.Valid(str)
		fmt.Println(b)
	}
}


/*
run:

true
false

*/

 



answered Jul 8, 2025 by avibootz
...