func validIPAddress(IP string) string {
    if checkIPv4(IP) {
        return "IPv4"
    }
    if checkIPv6(IP) {
        return "IPv6"
    }
    return "Neither"
}

func checkIPv4(IP string) bool {
   // 字符串这样切割 strs
:= strings.Split(IP, ".") if len(strs) != 4 { return false } for _, s := range strs { if len(s) == 0 || (len(s) > 1 && s[0] == '0') { return false }
// 直接访问字符串的值
if s[0] < '0' || s[0] > '9' { return false }
// 字符串转数字 n
, err := strconv.Atoi(s) if err != nil { return false } if n < 0 || n > 255 { return false } } return true } func checkIPv6(IP string) bool { strs := strings.Split(IP, ":") if len(strs) != 8 { return false } for _, s := range strs { if len(s) <= 0 || len(s) > 4 { return false } for i := 0; i < len(s); i++ { if s[i] >= '0' && s[i] <= '9' { continue } if s[i] >= 'A' && s[i] <= 'F' { continue } if s[i] >= 'a' && s[i] <= 'f' { continue } return false } } return true }

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-24
  • 2021-11-24
  • 2021-11-23
  • 2022-12-23
猜你喜欢
  • 2021-10-28
  • 2022-03-07
  • 2022-12-23
  • 2021-09-20
  • 2021-12-05
  • 2022-01-17
相关资源
相似解决方案