Standard library / encoding/binary
encoding/binary.Read()
Reads fixed-size values from a reader in the chosen byte order. Check errors before using the destination. Expected stdout: '258\n'.
Formal Syntax & Structure
go syntaxgo
func Read(r io.Reader, order ByteOrder, data any) errorParameters & Attributes
| Name | Type | Description |
|---|---|---|
| r | io.Reader | Input 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 v uint16 must(binary.Read(bytes.NewReader([]byte{1, 2}), binary.BigEndian, &v)) fmt.Println(v)}