aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/binary/example_test.go
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2017-11-14 12:38:11 -0500
committerRuss Cox <rsc@golang.org>2017-11-15 21:26:43 +0000
commit9232a612fed58d2629073056f827db3fae0cd5f0 (patch)
tree3a6ac2da9855b38345989f3055bf27601f1e55a1 /src/encoding/binary/example_test.go
parent4f6035ae13eba54051e5e46eb3c337b162f400ff (diff)
downloadgo-9232a612fed58d2629073056f827db3fae0cd5f0.tar.xz
encoding/binary: make new example a bit more idiomatic
Mainly get rid of the weird zero-value struct literal, but while we're here also group and order things a bit better: first the reader, then the data, then the call (which takes reader then data). Change-Id: I901b0661d85d8eaa0807e4482aac66500ca996c7 Reviewed-on: https://go-review.googlesource.com/78118 Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
Diffstat (limited to 'src/encoding/binary/example_test.go')
-rw-r--r--src/encoding/binary/example_test.go14
1 files changed, 8 insertions, 6 deletions
diff --git a/src/encoding/binary/example_test.go b/src/encoding/binary/example_test.go
index e99aef288d..6f892c2b8d 100644
--- a/src/encoding/binary/example_test.go
+++ b/src/encoding/binary/example_test.go
@@ -52,18 +52,20 @@ func ExampleRead() {
}
func ExampleRead_multi() {
- data := struct {
+ b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40, 0xff, 0x01, 0x02, 0x03, 0xbe, 0xef}
+ r := bytes.NewReader(b)
+
+ var 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 {
+ }
+
+ if err := binary.Read(r, binary.LittleEndian, &data); err != nil {
fmt.Println("binary.Read failed:", err)
}
+
fmt.Println(data.PI)
fmt.Println(data.Uate)
fmt.Printf("% x\n", data.Mine)