-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump.go
executable file
·58 lines (48 loc) · 957 Bytes
/
dump.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
// Package utils
package utils
import (
"bytes"
"encoding/json"
"fmt"
"io"
)
// DumpToString interface to string
func DumpToString(v interface{}) string {
str, ok := v.(string)
if !ok {
buff := &bytes.Buffer{}
json.NewEncoder(buff).Encode(v)
return buff.String()
}
return str
}
// DebugPrint for debug print on terminal
func DebugPrint(v ...interface{}) {
for _, x := range v {
fmt.Println(DumpToString(x))
}
}
// Data To json Bytes
func ToJSONByte(v interface{}) []byte {
switch v.(type) {
case []byte:
return v.([]byte)
case string:
return []byte(v.(string))
}
buff := &bytes.Buffer{}
json.NewEncoder(buff).Encode(v)
return buff.Bytes()
}
// StreamToByte cast
func StreamToByte(stream io.Reader) []byte {
buf := new(bytes.Buffer)
buf.ReadFrom(stream)
return buf.Bytes()
}
// StreamToString cast
func StreamToString(stream io.Reader) string {
buf := new(bytes.Buffer)
buf.ReadFrom(stream)
return buf.String()
}