首页 > 编程 > Golang > 正文

go语言版的ip2long函数实例

2020-04-01 19:20:02
字体:
来源:转载
供稿:网友
这篇文章主要介绍了go语言版的ip2long函数,实例分析了Go语言实现的ip2long函数技巧,具有一定参考借鉴价值,需要的朋友可以参考下
 

本文实例讲述了go语言版的ip2long函数。分享给大家供大家参考。具体分析如下:

这里介绍的go语言版的ip2long 函数不会对 IP 的合法性进行校验。

复制代码代码如下:
// 注意: 该函数不会对 IP 的合法性进行校验
func Ip2Long(ip string) (ips string) {
    var ip_pieces = strings.Split(ip, ".")
 ip_1, _ := strconv.ParseInt(ip_pieces[0], 10, 32)
 ip_2, _ := strconv.ParseInt(ip_pieces[1], 10, 32)
 ip_3, _ := strconv.ParseInt(ip_pieces[2], 10, 32)
 ip_4, _ := strconv.ParseInt(ip_pieces[3], 10, 32)
 var ip_bin string = fmt.Sprintf("%08b%08b%08b%08b", ip_1, ip_2, ip_3, ip_4)
 ip_int, _ := strconv.ParseInt(ip_bin, 2, 64)
 return
}

 

希望本文所述对大家的Go语言程序设计有所帮助。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表