MaxMind GeoIP database
A pure Go interface to the freeThis package provides fast lookups of country by IP address.
This implementation compiles an optimized version of the database into Go (using gobundle), and so does not rely on external files. The in-memory table size is around ~800KB.
Caveats
Currently only IPv4->country lookups are supported.
Installation
$ # Library
$ go get github.com/alecthomas/geoip
$ # Command
$ go get github.com/alecthomas/geoip/cmd/geoip
Usage
Command line usage:
$ geoip 1.1.1.1
Australia (AU)
See GoDoc for API documentation.
geo, err := geoip.New()
country := geo.Lookup(net.ParseIP("1.1.1.1"))
fmt.Printf("%s\n", country)
Performance
A benchmark is included (go test -bench='.*'
). On my 2GHz Core i7 each lookup takes around 550ns.
Updating the database
The package can be rebuilt with an updated datbase with the following commands:
$ curl -O http://www.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip
$ unzip GeoIPCountryCSV.zip
$ make
Legal
This product includes GeoLite data created by MaxMind, available from maxmind.com.