geoip

package module
v0.0.0-...-8355933 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 17, 2014 License: MIT Imports: 4 Imported by: 0

README

geoip

A simple Go wrapper for libGeoIP.

Installation

  1. Install libGeoIP. With Homebrew, use the geoip formula. On Debian/Ubuntu, install libgeoip-dev.
  2. go get github.com/codahale/geoip

Documentation

For documentation, check godoc.

Documentation

Overview

Package geoip provides a thin wrapper around libGeoIP for looking up geographical information about IP addresses.

Example
g, err := Open(*dbFile, nil)
if err != nil {
	panic(err)
}
defer g.Close()

fmt.Printf("%#v\n", g.Lookup("24.24.24.24"))
Output:

&geoip.Record{CountryCode:"US", CountryCode3:"USA", CountryName:"United States", Region:"NY", City:"Deer Park", PostalCode:"11729", Latitude:40.762699127197266, Longitude:-73.32270050048828, AreaCode:631, ContinentCode:"NA"}

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultOptions = &Options{
	Caching:        CacheDefault,
	ReloadOnUpdate: true,
	UseMMap:        true,
}

DefaultOptions caches no data, reloads on updates, and uses MMAP.

Functions

This section is empty.

Types

type CachingStrategy

type CachingStrategy int

CachingStrategy determines what data libGeoIP will cache.

const (
	// CacheDefault caches no data.
	CacheDefault CachingStrategy = C.GEOIP_STANDARD

	// CacheAll caches all data in memory.
	CacheAll CachingStrategy = C.GEOIP_MEMORY_CACHE

	// CacheMRU caches the most recently used data in memory.
	CacheMRU CachingStrategy = C.GEOIP_INDEX_CACHE
)

type Database

type Database struct {
	// contains filtered or unexported fields
}

A Database is a GeoIP database.

func Open

func Open(filename string, opts *Options) (*Database, error)

Open returns an open DB instance of the given .dat file. The result *must* be closed, or memory will leak.

func (*Database) Close

func (db *Database) Close() error

Close releases the resources allocated by the database.

func (*Database) Lookup

func (db *Database) Lookup(ip string) *Record

Lookup returns a GeoIP Record for the given IP address. If libGeoIP is > 1.5.0, this is thread-safe.

type Options

type Options struct {
	Caching        CachingStrategy // Caching determines what data will be cached.
	ReloadOnUpdate bool            // ReloadOnUpdate will watch the data files for updates.
	UseMMap        bool            // UseMMap enables MMAP for the data files.
	NoLocks        bool            // NoLocks depends on the libGeoIP for thread safety.
}

Options are the set of options provided by libGeoIP.

type Record

type Record struct {
	CountryCode   string  // CountryCode is a two-letter country code.
	CountryCode3  string  // CountryCode3 is a three-letter country code.
	CountryName   string  // CountryName is the name of the country.
	Region        string  // Region is the geographical region of the location.
	City          string  // City is the name of the city.
	PostalCode    string  // PostalCode is the location's postal code.
	Latitude      float64 // Latitude is the location's latitude.
	Longitude     float64 // Longitude is the location's longitude.
	AreaCode      int     // AreaCode is the location's area code.
	ContinentCode string  // ContinentCode is the location's continent.
}

Record is a GeoIP record.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL