-
Notifications
You must be signed in to change notification settings - Fork 5
/
helper.go
48 lines (42 loc) · 928 Bytes
/
helper.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
package log
import (
"encoding/json"
"encoding/xml"
)
// JSON is a helper function, following is its function code.
//
// data, _ := json.Marshal(v)
// return string(data)
func JSON(v interface{}) string {
pool := getBytesBufferPool()
buffer := pool.Get()
defer pool.Put(buffer)
buffer.Reset()
if err := json.NewEncoder(buffer).Encode(v); err != nil {
return ""
}
data := buffer.Bytes()
// remove the trailing newline
i := len(data) - 1
if i < 0 || i >= len(data) /* BCE */ {
return ""
}
if data[i] == '\n' {
data = data[:i]
}
return string(data)
}
// XML is a helper function, following is its function code.
//
// data, _ := xml.Marshal(v)
// return string(data)
func XML(v interface{}) string {
pool := getBytesBufferPool()
buffer := pool.Get()
defer pool.Put(buffer)
buffer.Reset()
if err := xml.NewEncoder(buffer).Encode(v); err != nil {
return ""
}
return string(buffer.Bytes())
}