Golang unicode/utf16.DecodeRune() function example

package unicode/utf16

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

DecodeRune returns the UTF-16 decoding of a surrogate pair. If the pair is not a valid UTF-16 surrogate pair, DecodeRune returns the Unicode replacement code point U+FFFD.

 package main

 import (
 "fmt"
 "unicode/utf16"
 )

 func main() {
 valid_pair := utf16.DecodeRune(0xd800, 0xdc00)
 fmt.Printf("%x \n", valid_pair)

 not_valid_pair := utf16.DecodeRune('\u6C34', '水')
 fmt.Printf("%x \n", not_valid_pair)

 not_valid_pair = utf16.DecodeRune(0xd800, '爱')
 fmt.Printf("%x \n", not_valid_pair)

 }

Sample output :

10000

fffd

fffd

Reference :

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

Advertisement