github.com/eun/go@v0.0.0-20170811110501-92cfd07a6cfd/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 ExampleByteOrder_put() {
    55  	b := make([]byte, 4)
    56  	binary.LittleEndian.PutUint16(b[0:], 0x03e8)
    57  	binary.LittleEndian.PutUint16(b[2:], 0x07d0)
    58  	fmt.Printf("% x\n", b)
    59  	// Output:
    60  	// e8 03 d0 07
    61  }
    62  
    63  func ExampleByteOrder_get() {
    64  	b := []byte{0xe8, 0x03, 0xd0, 0x07}
    65  	x1 := binary.LittleEndian.Uint16(b[0:])
    66  	x2 := binary.LittleEndian.Uint16(b[2:])
    67  	fmt.Printf("%#04x %#04x\n", x1, x2)
    68  	// Output:
    69  	// 0x03e8 0x07d0
    70  }
    71  
    72  func ExamplePutUvarint() {
    73  	buf := make([]byte, binary.MaxVarintLen64)
    74  
    75  	for _, x := range []uint64{1, 2, 127, 128, 255, 256} {
    76  		n := binary.PutUvarint(buf, x)
    77  		fmt.Printf("%x\n", buf[:n])
    78  	}
    79  	// Output:
    80  	// 01
    81  	// 02
    82  	// 7f
    83  	// 8001
    84  	// ff01
    85  	// 8002
    86  }
    87  
    88  func ExamplePutVarint() {
    89  	buf := make([]byte, binary.MaxVarintLen64)
    90  
    91  	for _, x := range []int64{-65, -64, -2, -1, 0, 1, 2, 63, 64} {
    92  		n := binary.PutVarint(buf, x)
    93  		fmt.Printf("%x\n", buf[:n])
    94  	}
    95  	// Output:
    96  	// 8101
    97  	// 7f
    98  	// 03
    99  	// 01
   100  	// 00
   101  	// 02
   102  	// 04
   103  	// 7e
   104  	// 8001
   105  }
   106  
   107  func ExampleUvarint() {
   108  	inputs := [][]byte{
   109  		[]byte{0x01},
   110  		[]byte{0x02},
   111  		[]byte{0x7f},
   112  		[]byte{0x80, 0x01},
   113  		[]byte{0xff, 0x01},
   114  		[]byte{0x80, 0x02},
   115  	}
   116  	for _, b := range inputs {
   117  		x, n := binary.Uvarint(b)
   118  		if n != len(b) {
   119  			fmt.Println("Uvarint did not consume all of in")
   120  		}
   121  		fmt.Println(x)
   122  	}
   123  	// Output:
   124  	// 1
   125  	// 2
   126  	// 127
   127  	// 128
   128  	// 255
   129  	// 256
   130  }
   131  
   132  func ExampleVarint() {
   133  	inputs := [][]byte{
   134  		[]byte{0x81, 0x01},
   135  		[]byte{0x7f},
   136  		[]byte{0x03},
   137  		[]byte{0x01},
   138  		[]byte{0x00},
   139  		[]byte{0x02},
   140  		[]byte{0x04},
   141  		[]byte{0x7e},
   142  		[]byte{0x80, 0x01},
   143  	}
   144  	for _, b := range inputs {
   145  		x, n := binary.Varint(b)
   146  		if n != len(b) {
   147  			fmt.Println("Varint did not consume all of in")
   148  		}
   149  		fmt.Println(x)
   150  	}
   151  	// Output:
   152  	// -65
   153  	// -64
   154  	// -2
   155  	// -1
   156  	// 0
   157  	// 1
   158  	// 2
   159  	// 63
   160  	// 64
   161  }