http://www.dotcoo.com/golang-ip2long-long2ip
其实也没什么难度的.写完放到这里分享下.
AddrToUint32
func AddrToUint32(addr net.Addr) (uint32, error) { var ip net.IP switch ipaddr := addr.(type) { case *net.IPAddr: ip = ipaddr.IP case *net.IPNet: ip = ipaddr.IP case *net.TCPAddr: ip = ipaddr.IP case *net.UDPAddr: ip = ipaddr.IP case *net.UnixAddr: return 0, errors.New("UnixAddr type not support") default: return 0, errors.New("addr type not support") } return binary.BigEndian.Uint32(ip.To4()), nil }
ip2long
func Ip2long(ipstr string) (ip uint32) { r := `^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})` reg, err := regexp.Compile(r) if err != nil { return } ips := reg.FindStringSubmatch(ipstr) if ips == nil { return } ip1, _ := strconv.Atoi(ips[1]) ip2, _ := strconv.Atoi(ips[2]) ip3, _ := strconv.Atoi(ips[3]) ip4, _ := strconv.Atoi(ips[4]) if ip1>255 || ip2>255 || ip3>255 || ip4 > 255 { return } ip += uint32(ip1 * 0x1000000) ip += uint32(ip2 * 0x10000) ip += uint32(ip3 * 0x100) ip += uint32(ip4) return }
long2ip
func Long2ip(ip uint32) string { return fmt.Sprintf("%d.%d.%d.%d", ip>>24, ip<<8>>24, ip<<16>>24, ip<<24>>24) }