tree - example_test.go
1 package tree_test
2
3 import (
4 "bytes"
5 "fmt"
6
7 "vimagination.zapto.org/tree"
8 )
9
10 func Example() {
11 var (
12 buf, readBuf bytes.Buffer
13 root tree.Branch
14 branch tree.Branch
15 )
16
17 root.Add("child1", tree.Leaf([]byte("Hello")))
18 root.Add("child2", tree.Leaf([]byte("World")))
19 root.Add("branch1", &branch)
20
21 branch.Add("childA", tree.Leaf([]byte("Foo")))
22 branch.Add("childB", tree.Leaf([]byte("Bar")))
23
24 tree.Serialise(&buf, root)
25
26 t := tree.OpenAt(bytes.NewReader(buf.Bytes()), int64(buf.Len()))
27
28 child1, _ := t.Child("child1")
29
30 child1.WriteTo(&readBuf)
31
32 fmt.Printf("child 1 data: %q\n", readBuf.Bytes())
33
34 readBuf.Reset()
35
36 branch1, _ := t.Child("branch1")
37 childB, _ := branch1.Child("childB")
38
39 childB.WriteTo(&readBuf)
40
41 fmt.Printf("child B data: %q\n", readBuf.Bytes())
42
43 // Output:
44 // child 1 data: "Hello"
45 // child B data: "Bar"
46 }
47