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) intParameters & Attributes
| Name | Type | Description |
|---|---|---|
| p | []byte | Writable destination with enough length for the encoded rune. |
| r | rune | Unicode 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)}