How to Handle Unicode and UTF-8 in Go

Go natively supports UTF-8 strings and uses runes for Unicode code points, requiring no manual encoding conversion.

Go handles Unicode and UTF-8 natively by representing all strings as UTF-8 byte sequences and using rune (an alias for int32) to represent individual Unicode code points. Use the unicode package for character classification and the utf8 package for encoding and decoding operations.

package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {
	s := "Hello δΈ–η•Œ"
	fmt.Println("String:", s)
	fmt.Println("Bytes:", len(s))
	fmt.Println("Runes:", utf8.RuneCountInString(s))

	for _, r := range s {
		fmt.Printf("Rune: %c (U+%04X)\n", r, r)
	}
}