-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test.go
52 lines (41 loc) · 1.12 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2022 Matt Schultz. All rights reserved.
// Use of this source code is governed by an ISC license that can be found in the LICENSE file.
package imsg_test
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"github.com/schultz-is/imsg-go"
)
// This example constructs an IMsg and encodes it into an imsg in the current system's byte order.
func ExampleEncoder_Encode() {
im, err := imsg.New(777, 888, 0, []byte("Howdy y'all!"))
if err != nil {
fmt.Println("error:", err)
return
}
var b bytes.Buffer
err = imsg.NewEncoder(&b).Encode(im)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(hex.Dump(b.Bytes()))
}
// This example constructs an IMsg by reading and decoding an imsg in little endian byte order.
func ExampleDecoder_Decode() {
b, err := hex.DecodeString("090300001c000000780300005bb70000486f776479207927616c6c21")
if err != nil {
fmt.Println("error:", err)
return
}
r := bytes.NewReader(b)
var im imsg.IMsg
err = imsg.NewDecoderWithByteOrder(r, binary.LittleEndian).Decode(&im)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Printf("%#v\n", im)
}