forked from kirinlabs/HttpRequest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
55 lines (51 loc) · 1.1 KB
/
utils.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
package HttpRequest
import (
"bytes"
"encoding/binary"
"encoding/json"
)
func Export(v interface{}) string {
b, err := json.Marshal(v)
if err != nil {
return ""
}
var buf bytes.Buffer
err = json.Indent(&buf, b, "", "\t")
if err != nil {
return ""
}
return buf.String()
}
func Json(v interface{}) string {
b, err := json.Marshal(v)
if err != nil {
return ""
}
return string(b)
}
func IntByte(v interface{}) []byte {
b := bytes.NewBuffer([]byte{})
switch v.(type) {
case int:
binary.Write(b, binary.BigEndian, int64(v.(int)))
case int8:
binary.Write(b, binary.BigEndian, v.(int8))
case int16:
binary.Write(b, binary.BigEndian, v.(int16))
case int32:
binary.Write(b, binary.BigEndian, v.(int32))
case int64:
binary.Write(b, binary.BigEndian, v.(int64))
case uint:
binary.Write(b, binary.BigEndian, uint64(v.(uint)))
case uint8:
binary.Write(b, binary.BigEndian, v.(uint8))
case uint16:
binary.Write(b, binary.BigEndian, v.(uint16))
case uint32:
binary.Write(b, binary.BigEndian, v.(uint32))
case uint64:
binary.Write(b, binary.BigEndian, v.(uint64))
}
return b.Bytes()
}