GitHub: https://github.com/haxqer/ip2geo 写这个库的起因很简单:服务里用 geoip2-golang 查 IP 归属地,profile 一看,单次 City() 要 1.7µs 左右,大部分时间花在解码 mmdb 的数据结构上。而我其实只需要国家 / 省份 / 城市 / 经纬度 / 时区这几个字段。
于是做了 ip2geo:把 MaxMind GeoIP2 / GeoLite2 City (或 DB-IP )的 .mmdb 一次性转成紧凑的 .geoc 文件,之后直接在它上面查 IPv4 / IPv6 。性能 Apple M3 单核,500 万个随机 IPv4 ,5 次取中位数: reader ns / 次 吞吐 相对 City() geoip2-golang City() 1768 0.6 M/s 1× geoip2-golang Country() 1121 0.9 M/s 1.6× ip2geo city profile 119 8.4 M/s 14.5× ip2geo country profile 30.5 32.8 M/s 58× IPv6 在 country profile 下约 42ns ,city profile 约 260ns 。
基准代码在仓库 bench/ 目录,可以自己复现。特点 读取端 只依赖标准库 ,零第三方依赖 Lookup 零内存分配 , *Reader 并发安全 三档 profile 按需取舍:country ( 3.5 MiB )/ region ( 26 MiB )/ city ( 116 MiB ),以 123 MiB 的 GeoIP2-City 为源 也可以不转换, mmdb.Open 直接读 .mmdb 在内存里建索引(启动约 2 秒),API 风格接近 geoip2-golang 附带 CLI: ip2geo build / ip2geo lookup 不用注册 MaxMind 也能试 Release 里有基于 DB-IP Lite ( CC BY 4.0 )构建好的 .geoc : curl -LO https://github.com/haxqer/ip2geo/releases/download/data-2026-10/dbip-city-lite.geoc.gz gunzip dbip-city-lite.geoc.gz go run github.com/haxqer/ip2geo/cmd/ip2geo@latest lookup -db dbip-city-lite.geoc 8.8.8.8 # 8.8.8.8: US / California / Mountain View (37.4220, -122.0850) 刚发布 v0.1.0 ,MIT 协议。
欢迎试用、提 issue ,觉得有用的话给个 star 🙏