-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
75 lines (64 loc) · 1.33 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package utils
import (
"bytes"
"encoding/json"
"errors"
"net"
"net/http"
"os"
"strconv"
"strings"
)
func Marshal(v interface{}) ([]byte, error) {
buff := bytes.NewBuffer([]byte{})
encoder := json.NewEncoder(buff)
encoder.SetEscapeHTML(false)
err := encoder.Encode(v)
return buff.Bytes(), err
}
func UnmarshalNumber(bt []byte, v interface{}) error {
if len(bt) == 0 {
return errors.New("bt is nil")
}
d := json.NewDecoder(bytes.NewReader(bt))
d.UseNumber()
if err := d.Decode(v); err != nil {
return err
}
return nil
}
//获取参数,不支持keys 0
func GetRequestKey(r *http.Request, key string) string {
return r.URL.Query().Get(key)
}
func GetCurrentPath() string {
currentPath, _ := os.Getwd()
return currentPath
}
func GetLocalIP() string {
addresses, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, address := range addresses {
// 检查ip地址判断是否回环地址
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return ""
}
func IPStringToInt(ipString string) int {
ipSegs := strings.Split(ipString, ".")
var ipInt int = 0
var pos uint = 24
for _, ipSeg := range ipSegs {
tempInt, _ := strconv.Atoi(ipSeg)
tempInt = tempInt << pos
ipInt = ipInt | tempInt
pos -= 8
}
return ipInt
}