git.zd.zone/hrpc/hrpc@v0.0.12/utils/uniqueid/nodeid.go (about) 1 package uniqueid 2 3 import ( 4 "errors" 5 "net" 6 "strconv" 7 "strings" 8 ) 9 10 // createNodeID will return a number range from 0 to 1023 11 // It can be temp used for identifying a node with an unique id 12 func createNodeID() (int64, error) { 13 addrs, err := net.InterfaceAddrs() 14 if err != nil { 15 // Cannot run this application because the IP addresses cannot be found 16 return 0, err 17 } 18 19 for _, addr := range addrs { 20 if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { 21 if ipnet.IP.To4() != nil { 22 myIP = ipnet.IP.String() 23 ipStr := strings.ReplaceAll(myIP, ".", "") 24 ipInt, _ := strconv.ParseInt(ipStr, 10, 64) 25 nodeID := ipInt % 1024 26 return nodeID, nil 27 } 28 } 29 } 30 31 return 0, errors.New("error") 32 }