---
# System prepended metadata

title: IP查詢省份
tags: [Golang]

---

---
title: IP查詢省份
---

# IP查詢省份
###### tags: `Golang`

### 相關文章網上很多，但是需要的lib不好找，特此紀錄下

這裡我把找到的lib放到自己的git。

未來需要更新的話再找找，這裡先提供急用。

我的範例git: https://github.com/ronnielin8862/go-practice/tree/master/pkg/ipGetLocation

dat檔案原git: https://github.com/out0fmemory/qqwry.dat

golang library: https://github.com/kayon/iploc/

```
func Test1(t *testing.T) {
	ip := "101.226.168.228"
	iPGetLocation(ip)
}

func iPGetLocation(ip string) {
	loc, err := iploc.Open("/Users/ronnie/Documents/coding/code/go-practice/pkg/ipGetLocation/qqwry.dat/qqwry_lastest.dat")
	if err != nil {
		panic(err)
	
    	detail = loc.Find(ip)
	fmt.Println(detail.String())
	fmt.Println(detail.Country, detail.Province, detail.City, detail.County)

	fmt.Println("22 = ", ConvertToString(detail.String(), "gbk", "utf-8"))
	fmt.Println("Country = ", ConvertToString(detail.Country, "gbk", "utf-8"))
	fmt.Println("Province = ", ConvertToString(detail.Province, "gbk", "utf-8"))
	fmt.Println("City = ", ConvertToString(detail.City, "gbk", "utf-8"))
	fmt.Println("County = ", ConvertToString(detail.County, "gbk", "utf-8"))
	fmt.Println("Region = ", ConvertToString(detail.Region, "gbk", "utf-8"))
	fmt.Println("ip = ", detail.IP)
}
```
console:
```
22 =  上海市 上海有孚计算机网络有限公司电信节点
Country =  上海市
Province =  
City =  
County =  
Region =  上海有孚计算机网络有限公司电信节点
ip =  101.226.168.228
```