forked from Meituan-Dianping/cat-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
38 lines (31 loc) · 713 Bytes
/
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
package cat
import (
"fmt"
"net"
"time"
)
func getLocalhostIp() (ip net.IP, err error) {
ip = net.IPv4(127, 0, 0, 1)
addrs, err := net.InterfaceAddrs()
if err != nil {
return
}
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
ip = ipnet.IP
return
}
}
}
return
}
func ip2String(ip net.IP) string {
return fmt.Sprintf("%d.%d.%d.%d", ip[12], ip[13], ip[14], ip[15])
}
func ip2HexString(ip net.IP) string {
return fmt.Sprintf("%02x%02x%02x%02x", ip[12], ip[13], ip[14], ip[15])
}
func duration2Millis(duration time.Duration) int64 {
return duration.Nanoseconds() / time.Millisecond.Nanoseconds()
}