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) errorParameters & Attributes
| Name | Type | Description |
|---|---|---|
| w | io.Writer | Output stream. |
| 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
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())}