In Go, character encoding and decoding, specifically UTF-8, can be handled using the unicode/utf8
package and the built-in string
type. Here's an example of how to encode and decode UTF-8 characters in Go:
utf8.EncodeRune()
function. Here's an example:package main
import (
"fmt"
"unicode/utf8"
)
func main() {
str := "Hello, 世界"
encoded := make([]byte, len(str)*3) // Allocate enough space for encoding
encodedLength := 0
for _, runeVal := range str {
runeLength := utf8.EncodeRune(encoded[encodedLength:], runeVal)
encodedLength += runeLength
}
fmt.Printf("Encoded: %x\n", encoded[:encodedLength])
}
Output:
Encoded: 48656c6c6f2c20e4b896e7958c
utf8.DecodeRune()
or utf8.DecodeLastRune()
functions. Here's an example:package main
import (
"fmt"
"unicode/utf8"
)
func main() {
encoded := []byte{72, 101, 108, 108, 111, 44, 32, 228, 184, 150, 231, 149, 140}
decoded := ""
remaining := encoded
for len(remaining) > 0 {
runeVal, runeLength := utf8.DecodeRune(remaining)
decoded += string(runeVal)
remaining = remaining[runeLength:]
}
fmt.Printf("Decoded: %s\n", decoded)
}
Output:
Decoded: Hello, 世界
Using these functions from the unicode/utf8
package, you can easily handle UTF-8 encoding and decoding in Go.