-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon_tools.go
More file actions
38 lines (32 loc) · 848 Bytes
/
common_tools.go
File metadata and controls
38 lines (32 loc) · 848 Bytes
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 gotools
import (
"net"
"strconv"
"strings"
)
func stringToInt(str string) int {
v, err := strconv.Atoi(str)
if err != nil {
panic("host address is wrong format! " + err.Error())
}
return v
}
// GetSeedForRandomCreation is to create the seed according to the host address
func GetSeedForRandomCreation() int64 {
addrs, err := net.InterfaceAddrs()
if err != nil {
panic("can't get the host addresses." + err.Error())
}
for _, address := range addrs {
// skip the loop address
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
ipSecs := strings.Split(ipnet.IP.To4().String(), ".")
ret := int64(stringToInt(ipSecs[0])*255*255*255 + stringToInt(ipSecs[1])*255*255 +
stringToInt(ipSecs[2])*255 + stringToInt(ipSecs[3]))
return ret
}
}
}
return 0
}