forked from njones/particle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
98 lines (73 loc) · 2.14 KB
/
example_test.go
File metadata and controls
98 lines (73 loc) · 2.14 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package particle
import (
"bytes"
"fmt"
"strings"
)
func ExampleNewDecoder() {
// Setup the struct and reader...
v := struct{ Name string }{}
r := strings.NewReader(`---
name: A NewDecoder Example
---
Content...`)
// Do the decoding...
output, err := NewDecoder(YAMLEncoding, r, &v)
if err != nil {
// handle errors here
fmt.Println(err)
}
// Read the content to a buffer so we can see it...
content := new(bytes.Buffer)
content.ReadFrom(output)
fmt.Printf("yaml: %+v\ncontent: %s", v, content)
// Output:
// yaml: {Name:A NewDecoder Example}
// content: Content...
}
func ExampleEncoding_DecodeString() {
// Setup the struct and reader...
v := struct{ Name string }{}
src := `+++
name = "A DecodeString Example"
+++
Content...`
// Do the decoding...
b, err := TOMLEncoding.DecodeString(src, &v)
if err != nil {
// handle errors here
fmt.Println(err)
}
// Read the content to a buffer so we can see it...
content := string(b)
fmt.Printf("toml: %+v\ncontent: %s", v, content)
// Output:
// toml: {Name:A DecodeString Example}
// content: Content...
}
func ExampleNewEncoder() {
// Setup the struct and reader...
v := struct{ Name string }{Name: "A NewEncoder Example"}
w := new(bytes.Buffer)
// Do the encoding...
out, err := NewEncoder(YAMLEncoding, w, v)
if err != nil {
// handle errors here
fmt.Println(err)
}
// Writing to the writer that we got back from the NewEncoder function
out.Write([]byte("Content..."))
// view the raw bytes
fmt.Printf("content: % x", w.String())
// output: content: 2d 2d 2d 0a 6e 61 6d 65 3a 20 41 20 4e 65 77 45 6e 63 6f 64 65 72 20 45 78 61 6d 70 6c 65 0a 2d 2d 2d 0a 0a 43 6f 6e 74 65 6e 74 2e 2e 2e
}
func ExampleEncoding_EncodeToString() {
// Setup the struct and reader...
v := struct{ Name string }{Name: "A EncodeToString Example"}
src := []byte("Content...")
// Do the decoding...
content := JSONEncoding.EncodeToString(src, &v)
// view the raw bytes
fmt.Printf("content: % x", content)
// output: content: 7b 0a 09 22 4e 61 6d 65 22 3a 20 22 41 20 45 6e 63 6f 64 65 54 6f 53 74 72 69 6e 67 20 45 78 61 6d 70 6c 65 22 0a 7d 0a 0a 43 6f 6e 74 65 6e 74 2e 2e 2e
}