Golang bufio.ReadRune() function example

package bufio

ReadRune reads a single UTF-8 encoded Unicode character and returns the rune and its size in bytes. If the encoded rune is invalid, it consumes one byte and returns unicode.ReplacementChar (U+FFFD) with a size of 1.

Golang bufio.ReadRune() function usage example

 utf8buf := []byte("世界")
 readbuffer := bytes.NewBuffer(utf8buf)
 reader := bufio.NewReader(readbuffer)
 rune, size, err := reader.ReadRune()
 if err == nil {
 fmt.Println(string(rune))
 fmt.Printf("%x, %d\n", rune, size)
 fmt.Printf("%x\n", utf8buf[:size])
 }

output :

4e16, 3

e4b896

Advertisement