How to handle character encoding and decoding (e.g., UTF-8) in Go?

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:

  1. Encoding UTF-8: You can convert a string to UTF-8 byte representation using the 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
  1. Decoding UTF-8: You can decode a UTF-8 byte slice back to a string using the 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.