IP地址合法性判断
判断IP地址是否是xxx.xxx.xxx.xxx:port
代码
package helper
import (
"net"
"regexp"
"strings"
)
func HostAddrCheck(addr string) bool {
items:=strings.Split(addr,":")
if items==nil||len(items)!=2{
return false
}
a:=net.ParseIP(items[0])
if a==nil{
return false
}
match, err := regexp.MatchString("^[0-9]*$", items[1])
if err!=nil{
return false
}
i,err:=StringToInt64(items[1])
if err!=nil {
return false
}
if i<0||i>65535 {
return false
}
if match==false{
return false
}
return true
}
例子
//合法
ok:=HostAddrCheck("192.168.10.25:10000")
fmt.Println(ok)
//不合法
ok=HostAddrCheck("192.168.10.25")
fmt.Println(ok)