byteio - example_test.go
1 package byteio_test
2
3 import (
4 "bytes"
5 "fmt"
6
7 "vimagination.zapto.org/byteio"
8 )
9
10 func Example() {
11 var buf bytes.Buffer
12
13 w := byteio.StickyBigEndianWriter{Writer: &buf}
14
15 // Write a uint32
16 w.WriteUint32(0xDEADBEEF)
17
18 // Write a String
19 w.WriteString8("hello")
20
21 // Write a Bool
22 w.WriteBool(true)
23
24 r := byteio.StickyBigEndianReader{Reader: &buf}
25
26 // Read the values back
27 fmt.Printf(
28 "Read uint32: 0x%X\n"+
29 "Read string: %q\n"+
30 "Read bool: %v\n",
31 r.ReadUint32(),
32 r.ReadString8(),
33 r.ReadBool(),
34 )
35
36 fmt.Printf("\nWrote bytes: %d\nRead bytes: %d\n", w.Count, r.Count)
37
38 // Output:
39 // Read uint32: 0xDEADBEEF
40 // Read string: "hello"
41 // Read bool: true
42 //
43 // Wrote bytes: 11
44 // Read bytes: 11
45 }
46