Standard library / encoding/binary
encoding/binary.Decode()
Decodes fixed-size data from a byte buffer into a pointer. The count reports consumed bytes. Expected stdout: '2 258\n'.
Formal Syntax & Structure
go syntaxgo
func Decode(buf []byte, order ByteOrder, data any) (int, error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| buf | []byte | Byte buffer used for the operation; append APIs extend it. |
| order | ByteOrder | Byte ordering used for fixed-width numbers. |
| data | any | Input bytes for XML, or fixed-size value / destination pointer for binary operations. |
Return Value
(int, error)
Byte count and error.
Working Example
Example Snippetgo
package main
import ( "encoding/binary" "fmt")
func must(e error) { if e != nil { panic(e) }}
func main() { var v uint16 n, e := binary.Decode([]byte{1, 2, 9}, binary.BigEndian, &v) must(e) fmt.Println(n, v)}