github.com/c9s/go@v0.0.0-20180120015821-984e81f64e0c/src/encoding/binary/example_test.go (about) 1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package binary_test 6 7 import ( 8 "bytes" 9 "encoding/binary" 10 "fmt" 11 "math" 12 ) 13 14 func ExampleWrite() { 15 buf := new(bytes.Buffer) 16 var pi float64 = math.Pi 17 err := binary.Write(buf, binary.LittleEndian, pi) 18 if err != nil { 19 fmt.Println("binary.Write failed:", err) 20 } 21 fmt.Printf("% x", buf.Bytes()) 22 // Output: 18 2d 44 54 fb 21 09 40 23 } 24 25 func ExampleWrite_multi() { 26 buf := new(bytes.Buffer) 27 var data = []interface{}{ 28 uint16(61374), 29 int8(-54), 30 uint8(254), 31 } 32 for _, v := range data { 33 err := binary.Write(buf, binary.LittleEndian, v) 34 if err != nil { 35 fmt.Println("binary.Write failed:", err) 36 } 37 } 38 fmt.Printf("%x", buf.Bytes()) 39 // Output: beefcafe 40 } 41 42 func ExampleRead() { 43 var pi float64 44 b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40} 45 buf := bytes.NewReader(b) 46 err := binary.Read(buf, binary.LittleEndian, &pi) 47 if err != nil { 48 fmt.Println("binary.Read failed:", err) 49 } 50 fmt.Print(pi) 51 // Output: 3.141592653589793 52 } 53 54 func ExampleRead_multi() { 55 b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40, 0xff, 0x01, 0x02, 0x03, 0xbe, 0xef} 56 r := bytes.NewReader(b) 57 58 var data struct { 59 PI float64 60 Uate uint8 61 Mine [3]byte 62 Too uint16 63 } 64 65 if err := binary.Read(r, binary.LittleEndian, &data); err != nil { 66 fmt.Println("binary.Read failed:", err) 67 } 68 69 fmt.Println(data.PI) 70 fmt.Println(data.Uate) 71 fmt.Printf("% x\n", data.Mine) 72 fmt.Println(data.Too) 73 // Output: 74 // 3.141592653589793 75 // 255 76 // 01 02 03 77 // 61374 78 } 79 80 func ExampleByteOrder_put() { 81 b := make([]byte, 4) 82 binary.LittleEndian.PutUint16(b[0:], 0x03e8) 83 binary.LittleEndian.PutUint16(b[2:], 0x07d0) 84 fmt.Printf("% x\n", b) 85 // Output: 86 // e8 03 d0 07 87 } 88 89 func ExampleByteOrder_get() { 90 b := []byte{0xe8, 0x03, 0xd0, 0x07} 91 x1 := binary.LittleEndian.Uint16(b[0:]) 92 x2 := binary.LittleEndian.Uint16(b[2:]) 93 fmt.Printf("%#04x %#04x\n", x1, x2) 94 // Output: 95 // 0x03e8 0x07d0 96 } 97 98 func ExamplePutUvarint() { 99 buf := make([]byte, binary.MaxVarintLen64) 100 101 for _, x := range []uint64{1, 2, 127, 128, 255, 256} { 102 n := binary.PutUvarint(buf, x) 103 fmt.Printf("%x\n", buf[:n]) 104 } 105 // Output: 106 // 01 107 // 02 108 // 7f 109 // 8001 110 // ff01 111 // 8002 112 } 113 114 func ExamplePutVarint() { 115 buf := make([]byte, binary.MaxVarintLen64) 116 117 for _, x := range []int64{-65, -64, -2, -1, 0, 1, 2, 63, 64} { 118 n := binary.PutVarint(buf, x) 119 fmt.Printf("%x\n", buf[:n]) 120 } 121 // Output: 122 // 8101 123 // 7f 124 // 03 125 // 01 126 // 00 127 // 02 128 // 04 129 // 7e 130 // 8001 131 } 132 133 func ExampleUvarint() { 134 inputs := [][]byte{ 135 []byte{0x01}, 136 []byte{0x02}, 137 []byte{0x7f}, 138 []byte{0x80, 0x01}, 139 []byte{0xff, 0x01}, 140 []byte{0x80, 0x02}, 141 } 142 for _, b := range inputs { 143 x, n := binary.Uvarint(b) 144 if n != len(b) { 145 fmt.Println("Uvarint did not consume all of in") 146 } 147 fmt.Println(x) 148 } 149 // Output: 150 // 1 151 // 2 152 // 127 153 // 128 154 // 255 155 // 256 156 } 157 158 func ExampleVarint() { 159 inputs := [][]byte{ 160 []byte{0x81, 0x01}, 161 []byte{0x7f}, 162 []byte{0x03}, 163 []byte{0x01}, 164 []byte{0x00}, 165 []byte{0x02}, 166 []byte{0x04}, 167 []byte{0x7e}, 168 []byte{0x80, 0x01}, 169 } 170 for _, b := range inputs { 171 x, n := binary.Varint(b) 172 if n != len(b) { 173 fmt.Println("Varint did not consume all of in") 174 } 175 fmt.Println(x) 176 } 177 // Output: 178 // -65 179 // -64 180 // -2 181 // -1 182 // 0 183 // 1 184 // 2 185 // 63 186 // 64 187 }