Skip to content

Commit

Permalink
增加控制台实例
Browse files Browse the repository at this point in the history
  • Loading branch information
mohong122 committed May 25, 2017
1 parent 0755357 commit 20cd77c
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 5 deletions.
39 changes: 34 additions & 5 deletions binding/golang/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
golang 实现ip地址查询
###golang 实现ip地址查询

获取
####获取

```
go get github.com/mohong122/ip2region/binding/golang
Expand All @@ -9,7 +9,7 @@ go get github.com/mohong122/ip2region/binding/golang



使用
####使用

```golang

Expand Down Expand Up @@ -38,7 +38,7 @@ func main() {

```

返回对象
####返回对象
```golang
type IpInfo struct {
CityId int64
Expand All @@ -50,11 +50,40 @@ type IpInfo struct {
}
```

性能
#### 性能

|名称|次数|平均耗时|
|---|---|------|
BenchmarkBtreeSearch-4| 200000 | 7715 ns/op
BenchmarkMemorySearch-4| 2000000 | 840 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
```
4 changes: 4 additions & 0 deletions binding/golang/ip2Region.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ type IpInfo struct {
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 {

lineSlice := strings.Split(string(line), "|")
Expand Down
70 changes: 70 additions & 0 deletions binding/golang/main.go
Original file line number Diff line number Diff line change
@@ -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()))
}
}
}

0 comments on commit 20cd77c

Please sign in to comment.