-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappend.go
More file actions
40 lines (32 loc) · 708 Bytes
/
append.go
File metadata and controls
40 lines (32 loc) · 708 Bytes
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
package parse
import (
"io"
"reflect"
)
// Write encoded value into output stream.
func Write(out io.Writer, value interface{}) error {
valueOf := reflect.ValueOf(value)
typeOf := valueOf.Type()
p, err := compile(typeOf, reflect.StructTag(""))
if err != nil {
return err
}
return p.WriteValue(out, valueOf)
}
type appender struct {
buf []byte
}
func (a *appender) Write(data []byte) (int, error) {
a.buf = append(a.buf, data...)
return len(data), nil
}
// Append encoded value to slice.
// Function returns new slice.
func Append(array []byte, value interface{}) ([]byte, error) {
x := &appender{array}
err := Write(x, value)
if err != nil {
return nil, err
}
return x.buf, nil
}