-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_test.go
67 lines (50 loc) · 1.13 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package bson_test
import (
"encoding/hex"
"fmt"
"github.com/cristalhq/bson"
)
func Example_types() {
arr := bson.A{"a", "b", "c", 12345}
doc := bson.D{{"hello", "world"}, {"answer", 42}}
doo := bson.M{"hello": "world", "pi": 3.14159}
fmt.Println(arr)
fmt.Println(arr.AsD())
fmt.Println()
fmt.Println(doc)
fmt.Println(doc.AsM())
fmt.Println()
fmt.Println(doo)
fmt.Println(doo.AsD())
_, _, _ = arr, doc, doo
// Output:
// [a b c 12345]
// [{0 a} {1 b} {2 c} {3 12345}]
//
// [{hello world} {answer 42}]
// map[answer:42 hello:world]
//
// map[hello:world pi:3.14159]
// [{hello world} {pi 3.14159}]
}
func ExampleMarshal() {
arr := bson.A{"a", "b", "c"}
b, err := bson.Marshal(arr)
if err != nil {
panic(err)
}
fmt.Println(hex.EncodeToString(b))
// Output:
// 2000000002300002000000610002310002000000620002320002000000630000
}
func ExampleMarshalTo() {
arr := bson.A{"a", "b", "c"}
buf := make([]byte, 0, 128)
buf, err := bson.MarshalTo(buf, arr)
if err != nil {
panic(err)
}
fmt.Println(hex.EncodeToString(buf))
// Output:
// 2000000002300002000000610002310002000000620002320002000000630000
}