]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/binary: add example for Read multi
authorMark Wolfe <mark@wolfe.id.au>
Tue, 8 Aug 2017 11:22:41 +0000 (21:22 +1000)
committerIan Lance Taylor <iant@golang.org>
Sat, 12 Aug 2017 01:17:13 +0000 (01:17 +0000)
Change-Id: I27ff99aa7abb070f6ae79c8f964aa9bd6a83b89d
Reviewed-on: https://go-review.googlesource.com/53730
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/encoding/binary/example_test.go

index a8b8dba6507706da837ddb1b68761d1abf21e308..e99aef288d3ccf28d237707b03e9e3f1b312fbea 100644 (file)
@@ -51,6 +51,30 @@ func ExampleRead() {
        // Output: 3.141592653589793
 }
 
+func ExampleRead_multi() {
+       data := struct {
+               PI   float64
+               Uate uint8
+               Mine [3]byte
+               Too  uint16
+       }{}
+       b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40, 0xff, 0x01, 0x02, 0x03, 0xbe, 0xef}
+       buf := bytes.NewReader(b)
+       err := binary.Read(buf, binary.LittleEndian, &data)
+       if err != nil {
+               fmt.Println("binary.Read failed:", err)
+       }
+       fmt.Println(data.PI)
+       fmt.Println(data.Uate)
+       fmt.Printf("% x\n", data.Mine)
+       fmt.Println(data.Too)
+       // Output:
+       // 3.141592653589793
+       // 255
+       // 01 02 03
+       // 61374
+}
+
 func ExampleByteOrder_put() {
        b := make([]byte, 4)
        binary.LittleEndian.PutUint16(b[0:], 0x03e8)