A pure Go interface to the free MaxMind GeoIP database
This 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.
Currently only IPv4->country lookups are supported.
$ # Library
$ go get github.com/alecthomas/geoip
$ # Command
$ go get github.com/alecthomas/geoip/cmd/geoip
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)
A benchmark is included (go test -bench='.*'
). On my 2GHz Core i7 each lookup takes around 550ns.
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
This product includes GeoLite data created by MaxMind, available from maxmind.com.