1
This commit is contained in:
parent
ed967899c4
commit
f913e480a2
33
local_ip.go
33
local_ip.go
@ -1,33 +0,0 @@
|
|||||||
package q5
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
)
|
|
||||||
|
|
||||||
func GetLocalIP() (string, error) {
|
|
||||||
interfaces, err := net.Interfaces()
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
for _, iface := range interfaces {
|
|
||||||
if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
addrs, err := iface.Addrs()
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
for _, addr := range addrs {
|
|
||||||
ipnet, ok := addr.(*net.IPNet)
|
|
||||||
if !ok || ipnet.IP.IsLoopback() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if ipnet.IP.To4() != nil {
|
|
||||||
return ipnet.IP.String(), nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return "", fmt.Errorf("无法获取本机有效的IP地址")
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
package q5
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestGetLocalIP(t *testing.T) {
|
|
||||||
ip, err := GetLocalIP()
|
|
||||||
if err != nil {
|
|
||||||
// 错误处理
|
|
||||||
t.Errorf("local ip %v error:%s", ip, err)
|
|
||||||
}
|
|
||||||
fmt.Printf("local ip:[%s]\n", ip)
|
|
||||||
}
|
|
41
sysutils.go
41
sysutils.go
@ -131,12 +131,8 @@ func FormatUnixDateEx(sec int64) string {
|
|||||||
return strTime
|
return strTime
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
|
||||||
maxBitNum = 63 // 最大的有效位号,根据 int64 的位宽确定
|
|
||||||
)
|
|
||||||
|
|
||||||
func HasBitFlag(val int64, bitNum int32) bool {
|
func HasBitFlag(val int64, bitNum int32) bool {
|
||||||
if bitNum < 0 || bitNum > maxBitNum {
|
if bitNum < 0 || bitNum > 63 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
mask := int64(1) << bitNum
|
mask := int64(1) << bitNum
|
||||||
@ -144,15 +140,46 @@ func HasBitFlag(val int64, bitNum int32) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func SetBitFlag(val *int64, bitNum int32) {
|
func SetBitFlag(val *int64, bitNum int32) {
|
||||||
if bitNum < 0 || bitNum > maxBitNum {
|
if bitNum < 0 || bitNum > 63 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
*val |= int64(1) << bitNum
|
*val |= int64(1) << bitNum
|
||||||
}
|
}
|
||||||
|
|
||||||
func UnSetBitFlag(val *int64, bitNum int32) {
|
func UnSetBitFlag(val *int64, bitNum int32) {
|
||||||
if bitNum < 0 || bitNum > maxBitNum {
|
if bitNum < 0 || bitNum > 63 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
*val &= ^(int64(1) << bitNum)
|
*val &= ^(int64(1) << bitNum)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetLocalIP() string {
|
||||||
|
interfaces, err := net.Interfaces()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
ipList := []string{}
|
||||||
|
for _, iface := range interfaces {
|
||||||
|
if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
addrs, err := iface.Addrs()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
for _, addr := range addrs {
|
||||||
|
ipnet, ok := addr.(*net.IPNet)
|
||||||
|
if !ok || ipnet.IP.IsLoopback() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if ipnet.IP.To4() != nil {
|
||||||
|
ipList = append(ipList, ipnet.IP.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(ipList) != 1 {
|
||||||
|
panic("GetLocalIP error")
|
||||||
|
} else {
|
||||||
|
return ipList[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user