Standard library / encoding/binary

encoding/binary.Write()

Writes fixed-size values to a stream using explicit byte ordering. Struct padding is not copied from memory. Expected stdout: '0102\n'.

Formal Syntax & Structure

go syntaxgo
func Write(w io.Writer, order ByteOrder, data any) error

Parameters & Attributes

NameTypeDescription
wio.WriterOutput stream.
orderByteOrderByte ordering used for fixed-width numbers.
dataanyInput bytes for XML, or fixed-size value / destination pointer for binary operations.

Return Value

error

Nil on success; otherwise the operation error.

Working Example

Example Snippetgo
package main
import (  "bytes"  "encoding/binary"  "fmt")
func must(e error) {  if e != nil {    panic(e)  }}
func main() {  var out bytes.Buffer  must(binary.Write(&out, binary.BigEndian, uint16(258)))  fmt.Printf("%x\n", out.Bytes())}
Go Reference