Standard library / encoding/binary
encoding/binary.Encode()
Encodes into caller-provided storage; insufficient space produces an error. Expected stdout: '2 0201\n'.
Formal Syntax & Structure
go syntaxgo
func Encode(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() { p := make([]byte, 2) n, e := binary.Encode(p, binary.LittleEndian, uint16(258)) must(e) fmt.Printf("%d %x\n", n, p)}