Golang unicode/utf16.Encode() function example

package unicode/utf16

Golang unicode/utf16.Encode() function usage example.

 package main

 import (
 "fmt"
 "unicode/utf16"
 )

 func main() {
 rune_array := []rune{'a', 'b', '好', 0xfffd}

 var uint16_array []uint16

 uint16_array = utf16.Encode(rune_array)

 fmt.Println(uint16_array)

 // 0xfffd should not be encoded.

 for _, item := range uint16_array {
 fmt.Printf("0x%x \n", item)
 }
 }

Sample output :

[97 98 22909 65533]

0x61

0x62

0x597d

0xfffd

Reference :

https://golang.org/pkg/unicode/utf16/#Encode

Advertisement