根据手机号的前7位,查出对应的省份,城市,邮编,区号,手机卡的类型 想法来源: lovedboy/phone
go get -u github.com/deloz/phone
phoneInfo, err := phone.Find("1888888")
if err != nil {
fmt.Println(err)
} else {
fmt.Println(phoneInfo)
}
go test -v
&{Phone:1888888 Province:北京 City:北京 ZipCode:100000 AreaCode:0 PhoneType:移动 PhoneRecordCount:307990}
13* , 15* , 18* , 14[5,7] , 17[0,6,7,8]
| 4 bytes | <- phone.dat 版本号
------------
| 4 bytes | <- 第一个索引的偏移
-----------------------
| offset - 8 | <- 记录区
-----------------------
| index | <- 索引区
-----------------------
头部
头部为8
个字节,版本号为4
个字节,第一个索引的偏移为4
个字节。记录区
中每条记录的格式为<省份>|<城市>|<邮编>|<长途区号>\0
。 每条记录以\0
结束。索引区
中每条记录的格式为<手机号前七位(长4字节)><记录区的偏移(长4字节)><卡类型(长1字节)>
,每个索引的长度为9
个字节。
- 解析头部
8
个字节,得到索引区的第一条索引的偏移。 - 在索引区用二分查找得出
手机号在记录区的记录偏移
。 - 在记录区从上一步得到的
记录偏移处取数据
,直到遇到\0
。
- 1 移动
- 2 联通
- 3 电信
- 4 电信虚拟运营商
- 5 联通虚拟运营商
- 6 移动虚拟运营商
- Fork it ( https://github.com/deloz/phone/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
The MIT License (MIT) Copyright (c) 2015 Deloz