Standard library / unicode/utf8

utf8.EncodeRune()

Writes the UTF-8 encoding into p and returns its byte count. Buffer must be large enough; invalid rune encodes RuneError. Expected stdout: 'c3a9 2\n'.

Formal Syntax & Structure

go syntaxgo
func EncodeRune(p []byte, r rune) int

Parameters & Attributes

NameTypeDescription
p[]byteWritable destination with enough length for the encoded rune.
rruneUnicode code point.

Return Value

int

Writes the UTF-8 encoding into p and returns its byte count. Buffer must be large enough; invalid rune encodes RuneError.

Working Example

Example Snippetgo
package main
import (  "fmt"  "unicode/utf8")
func main() {  p := make([]byte, utf8.UTFMax)  n := utf8.EncodeRune(p, 'é')  fmt.Printf("%x %d\n", p[:n], n)}
Go Reference