aboutsummaryrefslogtreecommitdiff
path: root/src/pkg/encoding/binary
diff options
context:
space:
mode:
authorAndrew Gerrand <adg@golang.org>2011-12-20 13:16:36 +1100
committerAndrew Gerrand <adg@golang.org>2011-12-20 13:16:36 +1100
commit7bffdc724799c240ea796f3c7f5d1cbbb1e84d01 (patch)
tree89e5c7db6cab51ead7e1045d643b34b0ac7000e8 /src/pkg/encoding/binary
parent5842f7e46a278cbf75e37f8a489e1f02e4acc8ac (diff)
downloadgo-7bffdc724799c240ea796f3c7f5d1cbbb1e84d01.tar.xz
encoding/binary: add Write and Read examples
R=golang-dev, r, adg CC=golang-dev https://golang.org/cl/5495095
Diffstat (limited to 'src/pkg/encoding/binary')
-rw-r--r--src/pkg/encoding/binary/example_test.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/pkg/encoding/binary/example_test.go b/src/pkg/encoding/binary/example_test.go
new file mode 100644
index 0000000000..297d6c1ae3
--- /dev/null
+++ b/src/pkg/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"
+)
+
+// 18 2d 44 54 fb 21 09 40
+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())
+}
+
+// cafebabe
+func ExampleWrite_multi() {
+ buf := new(bytes.Buffer)
+ var data = []interface{}{
+ int8(-54),
+ uint8(254),
+ uint16(48826),
+ }
+ 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())
+}
+
+// 3.141592653589793
+func ExampleRead() {
+ var pi float64
+ b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40}
+ buf := bytes.NewBuffer(b)
+ err := binary.Read(buf, binary.LittleEndian, &pi)
+ if err != nil {
+ fmt.Println("binary.Read failed:", err)
+ }
+ fmt.Print(pi)
+}