Skip to content

Commit 812124a

Browse files
wolfeidauianlancetaylor
authored andcommitted
encoding/binary: add example for Read multi
Change-Id: I27ff99aa7abb070f6ae79c8f964aa9bd6a83b89d Reviewed-on: https://go-review.googlesource.com/53730 Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 590c5b0 commit 812124a

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/encoding/binary/example_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,30 @@ func ExampleRead() {
5151
// Output: 3.141592653589793
5252
}
5353

54+
func ExampleRead_multi() {
55+
data := struct {
56+
PI float64
57+
Uate uint8
58+
Mine [3]byte
59+
Too uint16
60+
}{}
61+
b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40, 0xff, 0x01, 0x02, 0x03, 0xbe, 0xef}
62+
buf := bytes.NewReader(b)
63+
err := binary.Read(buf, binary.LittleEndian, &data)
64+
if err != nil {
65+
fmt.Println("binary.Read failed:", err)
66+
}
67+
fmt.Println(data.PI)
68+
fmt.Println(data.Uate)
69+
fmt.Printf("% x\n", data.Mine)
70+
fmt.Println(data.Too)
71+
// Output:
72+
// 3.141592653589793
73+
// 255
74+
// 01 02 03
75+
// 61374
76+
}
77+
5478
func ExampleByteOrder_put() {
5579
b := make([]byte, 4)
5680
binary.LittleEndian.PutUint16(b[0:], 0x03e8)

0 commit comments

Comments
 (0)