All Projects → alecthomas → Geoip

alecthomas / Geoip

A pure Go interface to the free MaxMind GeoIP database

Programming Languages

go
31211 projects - #10 most used programming language

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.

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].