golang IPV4 转 Int64

// IP4toInt IPV4 转 Int64
func IP4toInt(IPv4Addr net.IP) int64 {
	if len(IPv4Addr) == 0 {
		return 0
	}
	bits := strings.Split(IPv4Addr.String(), ".")
	b0, _ := strconv.Atoi(bits[0])
	b1, _ := strconv.Atoi(bits[1])
	b2, _ := strconv.Atoi(bits[2])
	b3, _ := strconv.Atoi(bits[3])
	var sum int64
	// left shifting 24,16,8,0 and bitwise OR
	sum += int64(b0) << 24
	sum += int64(b1) << 16
	sum += int64(b2) << 8
	sum += int64(b3)
	return sum
}
string 类型的ip转 net.IP

net.ParseIP(ip)

调用:

int64IP:=IP4toInt(net.ParseIP(ip))

你可能感兴趣的:(golang,开发语言,后端)