commit
979e9bdda7
|
|
@ -1,6 +1,6 @@
|
||||||
golang 实现ip地址查询
|
###golang 实现ip地址查询
|
||||||
|
|
||||||
获取
|
####获取
|
||||||
|
|
||||||
```
|
```
|
||||||
go get github.com/mohong122/ip2region/binding/golang
|
go get github.com/mohong122/ip2region/binding/golang
|
||||||
|
|
@ -9,7 +9,7 @@ go get github.com/mohong122/ip2region/binding/golang
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
使用
|
####使用
|
||||||
|
|
||||||
```golang
|
```golang
|
||||||
|
|
||||||
|
|
@ -38,7 +38,7 @@ func main() {
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
返回对象
|
####返回对象
|
||||||
```golang
|
```golang
|
||||||
type IpInfo struct {
|
type IpInfo struct {
|
||||||
CityId int64
|
CityId int64
|
||||||
|
|
@ -50,7 +50,7 @@ type IpInfo struct {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
性能
|
#### 性能
|
||||||
|
|
||||||
|名称|次数|平均耗时|
|
|名称|次数|平均耗时|
|
||||||
|---|---|------|
|
|---|---|------|
|
||||||
|
|
@ -58,3 +58,32 @@ BenchmarkBtreeSearch-4| 200000 | 7715 ns/op
|
||||||
BenchmarkMemorySearch-4| 2000000 | 840 ns/op
|
BenchmarkMemorySearch-4| 2000000 | 840 ns/op
|
||||||
BenchmarkBinarySearch-4| 30000 | 42680 ns/op
|
BenchmarkBinarySearch-4| 30000 | 42680 ns/op
|
||||||
|
|
||||||
|
|
||||||
|
#### 测试程序
|
||||||
|
|
||||||
|
```
|
||||||
|
cd /binging/golang
|
||||||
|
|
||||||
|
go run main.go ../../data/ip2Region.db
|
||||||
|
|
||||||
|
Or
|
||||||
|
|
||||||
|
go build -o ip2region main.go
|
||||||
|
./ip2region ../../data/ip2region.db
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
会看到如下界面
|
||||||
|
|
||||||
|
```
|
||||||
|
initializing
|
||||||
|
+-------------------------------------------------------+
|
||||||
|
| ip2region test script |
|
||||||
|
| format 'ip type' |
|
||||||
|
| type option 'b-tree','binary','memory' default b-tree |
|
||||||
|
| Type 'quit' to exit program |
|
||||||
|
+-------------------------------------------------------+
|
||||||
|
ip2reginon >> 127.0.0.1 memory
|
||||||
|
0|未分配或者内网IP|0|0|0|0 960.5µs
|
||||||
|
|
||||||
|
```
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,10 @@ type IpInfo struct {
|
||||||
ISP string
|
ISP string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ip IpInfo)String() string {
|
||||||
|
return strconv.FormatInt(ip.CityId, 10) + "|" + ip.Country + "|" + ip.Region + "|" + ip.Province + "|" + ip.City + "|" + ip.ISP
|
||||||
|
}
|
||||||
|
|
||||||
func getIpInfo(cityId int64, line []byte) IpInfo {
|
func getIpInfo(cityId int64, line []byte) IpInfo {
|
||||||
|
|
||||||
lineSlice := strings.Split(string(line), "|")
|
lineSlice := strings.Split(string(line), "|")
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,70 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"github.com/mohong122/ip2region/binding/golang"
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
db := os.Args[1]
|
||||||
|
|
||||||
|
_,err:= os.Stat(db)
|
||||||
|
if os.IsNotExist(err){
|
||||||
|
panic("not found db " + db)
|
||||||
|
}
|
||||||
|
|
||||||
|
region, err := ip2region.New(db)
|
||||||
|
defer region.Close()
|
||||||
|
fmt.Println(`initializing
|
||||||
|
+-------------------------------------------------------+
|
||||||
|
| ip2region test script |
|
||||||
|
| format 'ip type' |
|
||||||
|
| type option 'b-tree','binary','memory' default b-tree |
|
||||||
|
| Type 'quit' to exit program |
|
||||||
|
+-------------------------------------------------------+`)
|
||||||
|
|
||||||
|
reader := bufio.NewReader(os.Stdin)
|
||||||
|
for {
|
||||||
|
fmt.Print("ip2reginon >> ")
|
||||||
|
data, _, _ := reader.ReadLine()
|
||||||
|
begin:= time.Now()
|
||||||
|
commands := strings.Fields(string(data))
|
||||||
|
ip := ip2region.IpInfo{}
|
||||||
|
len := len(commands)
|
||||||
|
if len == 0{
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if commands[0] == "quit"{
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if !(len > 1) {
|
||||||
|
commands = append(commands, "b-tree")
|
||||||
|
}
|
||||||
|
switch commands[1] {
|
||||||
|
case "b-tree":
|
||||||
|
ip, err = region.BtreeSearch(commands[0])
|
||||||
|
case "binary":
|
||||||
|
ip, err = region.BinarySearch(commands[0])
|
||||||
|
case "memory":
|
||||||
|
ip, err = region.MemorySearch(commands[0])
|
||||||
|
default:
|
||||||
|
err = errors.New("parameter error")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
|
||||||
|
fmt.Println( fmt.Sprintf("\x1b[0;31m%s\x1b[0m",err.Error()))
|
||||||
|
}else{
|
||||||
|
fmt.Println( fmt.Sprintf("\x1b[0;32m%s %s\x1b[0m",ip.String(),time.Since(begin).String()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue