github.com/mattn/go@v0.0.0-20171011075504-07f7db3ea99f/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 data := struct { 56 PI float64 57 Uate uint8 58 Mine [3]byte 59 Too uint16 60 }{} 61 b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40, 0xff, 0x01, 0x02, 0x03, 0xbe, 0xef} 62 buf := bytes.NewReader(b) 63 err := binary.Read(buf, binary.LittleEndian, &data) 64 if err != nil { 65 fmt.Println("binary.Read failed:", err) 66 } 67 fmt.Println(data.PI) 68 fmt.Println(data.Uate) 69 fmt.Printf("% x\n", data.Mine) 70 fmt.Println(data.Too) 71 // Output: 72 // 3.141592653589793 73 // 255 74 // 01 02 03 75 // 61374 76 } 77 78 func ExampleByteOrder_put() { 79 b := make([]byte, 4) 80 binary.LittleEndian.PutUint16(b[0:], 0x03e8) 81 binary.LittleEndian.PutUint16(b[2:], 0x07d0) 82 fmt.Printf("% x\n", b) 83 // Output: 84 // e8 03 d0 07 85 } 86 87 func ExampleByteOrder_get() { 88 b := []byte{0xe8, 0x03, 0xd0, 0x07} 89 x1 := binary.LittleEndian.Uint16(b[0:]) 90 x2 := binary.LittleEndian.Uint16(b[2:]) 91 fmt.Printf("%#04x %#04x\n", x1, x2) 92 // Output: 93 // 0x03e8 0x07d0 94 } 95 96 func ExamplePutUvarint() { 97 buf := make([]byte, binary.MaxVarintLen64) 98 99 for _, x := range []uint64{1, 2, 127, 128, 255, 256} { 100 n := binary.PutUvarint(buf, x) 101 fmt.Printf("%x\n", buf[:n]) 102 } 103 // Output: 104 // 01 105 // 02 106 // 7f 107 // 8001 108 // ff01 109 // 8002 110 } 111 112 func ExamplePutVarint() { 113 buf := make([]byte, binary.MaxVarintLen64) 114 115 for _, x := range []int64{-65, -64, -2, -1, 0, 1, 2, 63, 64} { 116 n := binary.PutVarint(buf, x) 117 fmt.Printf("%x\n", buf[:n]) 118 } 119 // Output: 120 // 8101 121 // 7f 122 // 03 123 // 01 124 // 00 125 // 02 126 // 04 127 // 7e 128 // 8001 129 } 130 131 func ExampleUvarint() { 132 inputs := [][]byte{ 133 []byte{0x01}, 134 []byte{0x02}, 135 []byte{0x7f}, 136 []byte{0x80, 0x01}, 137 []byte{0xff, 0x01}, 138 []byte{0x80, 0x02}, 139 } 140 for _, b := range inputs { 141 x, n := binary.Uvarint(b) 142 if n != len(b) { 143 fmt.Println("Uvarint did not consume all of in") 144 } 145 fmt.Println(x) 146 } 147 // Output: 148 // 1 149 // 2 150 // 127 151 // 128 152 // 255 153 // 256 154 } 155 156 func ExampleVarint() { 157 inputs := [][]byte{ 158 []byte{0x81, 0x01}, 159 []byte{0x7f}, 160 []byte{0x03}, 161 []byte{0x01}, 162 []byte{0x00}, 163 []byte{0x02}, 164 []byte{0x04}, 165 []byte{0x7e}, 166 []byte{0x80, 0x01}, 167 } 168 for _, b := range inputs { 169 x, n := binary.Varint(b) 170 if n != len(b) { 171 fmt.Println("Varint did not consume all of in") 172 } 173 fmt.Println(x) 174 } 175 // Output: 176 // -65 177 // -64 178 // -2 179 // -1 180 // 0 181 // 1 182 // 2 183 // 63 184 // 64 185 }