Standard library / encoding/hex
encoding/hex.Decode()
Decodes pairs of hexadecimal digits into a caller-provided slice. Check both count and error. Expected stdout: '2 Go\n'.
Formal Syntax & Structure
go syntaxgo
func Decode(dst, src []byte) (int, error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| dst | []byte | Destination byte slice; append APIs preserve its existing prefix. |
| src | []byte | Source bytes to encode or decode. |
Return Value
(int, error)
Byte count and error.
Working Example
Example Snippetgo
package main
import ( "encoding/hex" "fmt")
func must(e error) { if e != nil { panic(e) }}
func main() { p := make([]byte, 2) n, e := hex.Decode(p, []byte("476F")) must(e) fmt.Println(n, string(p[:n]))}