summaryrefslogtreecommitdiff
path: root/src/encoding/binary/example_test.go
diff options
context:
space:
mode:
authorTianon Gravi <admwiggin@gmail.com>2015-01-15 12:50:40 -0700
committerTianon Gravi <admwiggin@gmail.com>2015-01-15 12:50:40 -0700
commit2a0db60599fdd75b1bc3e297180fbe1282763759 (patch)
tree68d43c3e30d9ab961ddf6b7365201ca6b675b253 /src/encoding/binary/example_test.go
parentef33cba3c8de6c431df56503df51fcd3a473c89e (diff)
parentf154da9e12608589e8d5f0508f908a0c3e88a1bb (diff)
downloadgolang-2a0db60599fdd75b1bc3e297180fbe1282763759.tar.gz
Merge tag 'upstream/1.4' into debian-experimental
* tag 'upstream/1.4': Imported Upstream version 1.4
Diffstat (limited to 'src/encoding/binary/example_test.go')
-rw-r--r--src/encoding/binary/example_test.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/encoding/binary/example_test.go b/src/encoding/binary/example_test.go
new file mode 100644
index 000000000..067cf553b
--- /dev/null
+++ b/src/encoding/binary/example_test.go
@@ -0,0 +1,52 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package binary_test
+
+import (
+ "bytes"
+ "encoding/binary"
+ "fmt"
+ "math"
+)
+
+func ExampleWrite() {
+ buf := new(bytes.Buffer)
+ var pi float64 = math.Pi
+ err := binary.Write(buf, binary.LittleEndian, pi)
+ if err != nil {
+ fmt.Println("binary.Write failed:", err)
+ }
+ fmt.Printf("% x", buf.Bytes())
+ // Output: 18 2d 44 54 fb 21 09 40
+}
+
+func ExampleWrite_multi() {
+ buf := new(bytes.Buffer)
+ var data = []interface{}{
+ uint16(61374),
+ int8(-54),
+ uint8(254),
+ }
+ for _, v := range data {
+ err := binary.Write(buf, binary.LittleEndian, v)
+ if err != nil {
+ fmt.Println("binary.Write failed:", err)
+ }
+ }
+ fmt.Printf("%x", buf.Bytes())
+ // Output: beefcafe
+}
+
+func ExampleRead() {
+ var pi float64
+ b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40}
+ buf := bytes.NewReader(b)
+ err := binary.Read(buf, binary.LittleEndian, &pi)
+ if err != nil {
+ fmt.Println("binary.Read failed:", err)
+ }
+ fmt.Print(pi)
+ // Output: 3.141592653589793
+}