go語言版的ip2long函數(shù)實例
更新時間:2015年02月24日 15:14:01 作者:不是JS
這篇文章主要介紹了go語言版的ip2long函數(shù),實例分析了Go語言實現(xiàn)的ip2long函數(shù)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了go語言版的ip2long函數(shù)。分享給大家供大家參考。具體分析如下:
這里介紹的go語言版的ip2long 函數(shù)不會對 IP 的合法性進行校驗。
復(fù)制代碼 代碼如下:
// 注意: 該函數(shù)不會對 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
}
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語言程序設(shè)計有所幫助。
您可能感興趣的文章:
- golang實現(xiàn)unicode轉(zhuǎn)換為字符串string的方法
- Golang學(xué)習筆記(六):struct
- 簡單了解Go語言中函數(shù)作為值以及函數(shù)閉包的使用
- Go語言中函數(shù)的參數(shù)傳遞與調(diào)用的基本方法
- 舉例詳解Go語言中os庫的常用函數(shù)用法
- Go語言的os包中常用函數(shù)初步歸納
- Go語言常見哈希函數(shù)的使用
- Go語言里的new函數(shù)用法分析
- Go語言截取字符串函數(shù)用法
- Go語言中append函數(shù)用法分析
- GO語言延遲函數(shù)defer用法分析
- Go語言中的流程控制結(jié)構(gòu)和函數(shù)詳解
- golang中strconv.ParseInt函數(shù)用法示例
相關(guān)文章
Go語言實現(xiàn)生產(chǎn)者-消費者模式的方法總結(jié)
這篇文章主要介紹了在?Go?語言中實現(xiàn)生產(chǎn)者消費者模式的多種方法,并重點探討了通道、條件變量的適用場景和優(yōu)缺點,需要的可參考一下2023-05-05
Go語言并發(fā)編程之互斥鎖Mutex和讀寫鎖RWMutex
Go 語言中提供了很多同步工具,本文將介紹互斥鎖Mutex和讀寫鎖RWMutex的使用方法,想要具體了解的小伙伴,請參考下面文章詳細內(nèi)容,希望對你有所幫助2021-10-10
Golang中Gin數(shù)據(jù)庫表名前綴的三種方法
本文主要介紹了Golang中Gin數(shù)據(jù)庫表名前綴的三種方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2025-02-02

