Skip to content

Commit

Permalink
自定义phone.dat目录(env PHONE_DATA_DIR) @liut
Browse files Browse the repository at this point in the history
 Changes to be committed:
	modified:   README.md
	modified:   phonedata.go
  • Loading branch information
xluohome committed Mar 24, 2017
1 parent 5a43e68 commit 54154f7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

- 归属地信息库文件大小:3,203,029 字节
- 归属地信息库最后更新:2017年1月
- 手机号记录条数:354522
- 手机号段记录条数:354522

### phone.dat文件格式

Expand Down Expand Up @@ -54,7 +54,7 @@ Thinkpad s3 (Intel(R) Core(TM) i5-4200U CPU @ 1.60GHz)
```
go test -v --bench="."

BenchmarkFindPhone-4 200000 7320 ns/op
BenchmarkFindPhone-4 200000 6909 ns/op

```
Expand All @@ -66,6 +66,8 @@ python: https://github.com/lovedboy/phone
php : https://github.com/shitoudev/phone-location , https://github.com/iwantofun/php_phone
php ext: https://github.com/jonnywang/phone
java: https://github.com/fengjiajie/phone-number-geo
### 感谢
Expand Down
29 changes: 17 additions & 12 deletions phonedata.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"runtime"
"strconv"
Expand All @@ -26,11 +27,11 @@ const (
)

type PhoneRecord struct {
PhoneNum []byte
Province []byte
City []byte
ZipCode []byte
AreaZone []byte
PhoneNum string
Province string
City string
ZipCode string
AreaZone string
CardType string
}

Expand All @@ -47,9 +48,13 @@ var (
)

func init() {
_, fulleFilename, _, _ := runtime.Caller(0)
dir := os.Getenv("PHONE_DATA_DIR")
if dir == "" {
_, fulleFilename, _, _ := runtime.Caller(0)
dir = path.Dir(fulleFilename)
}
var err error
content, err = ioutil.ReadFile(path.Join(path.Dir(fulleFilename), PHONE_DAT))
content, err = ioutil.ReadFile(path.Join(dir, PHONE_DAT))
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -129,11 +134,11 @@ func Find(phone_num string) (pr *PhoneRecord, err error) {
card_str = "未知电信运营商"
}
pr = &PhoneRecord{
PhoneNum: []byte(phone_num),
Province: data[0],
City: data[1],
ZipCode: data[2],
AreaZone: data[3],
PhoneNum: phone_num,
Province: string(data[0]),
City: string(data[1]),
ZipCode: string(data[2]),
AreaZone: string(data[3]),
CardType: card_str,
}
return
Expand Down

0 comments on commit 54154f7

Please sign in to comment.