geoip2
packagemoduleThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
README¶
GeoIP2 Reader for Go
This library reads MaxMindGeoLite2andGeoIP2 databases.
This library is built usingthe Go maxminddb reader.All data for the database record is decoded using this library. If you onlyneed several fields, you may get superior performance by using maxminddb'sLookup directly with a result struct that only contains the required fields.(Seeexample_test.goin the maxminddb repository for an example of this.)
Installation
go get github.com/oschwald/geoip2-golangUsage
See GoDoc fordocumentation and examples.
Example
package mainimport ("fmt""log""net""github.com/oschwald/geoip2-golang")func main() {db, err := geoip2.Open("GeoIP2-City.mmdb")if err != nil {log.Fatal(err)}defer db.Close()// If you are using strings that may be invalid, check that ip is not nilip := net.ParseIP("81.2.69.142")record, err := db.City(ip)if err != nil {log.Fatal(err)}fmt.Printf("Portuguese (BR) city name: %v\n", record.City.Names["pt-BR"])if len(record.Subdivisions) > 0 {fmt.Printf("English subdivision name: %v\n", record.Subdivisions[0].Names["en"])}fmt.Printf("Russian country name: %v\n", record.Country.Names["ru"])fmt.Printf("ISO country code: %v\n", record.Country.IsoCode)fmt.Printf("Time zone: %v\n", record.Location.TimeZone)fmt.Printf("Coordinates: %v, %v\n", record.Location.Latitude, record.Location.Longitude)// Output:// Portuguese (BR) city name: Londres// English subdivision name: England// Russian country name: Великобритания// ISO country code: GB// Time zone: Europe/London// Coordinates: 51.5142, -0.0931}Testing
Make sure you checked out test data submodule:
git submodule initgit submodule updateExecute test suite:
go testContributing
Contributions welcome! Please fork the repository and open a pull requestwith your changes.
License
This is free software, licensed under the ISC license.
Documentation¶
Overview¶
Package geoip2 provides an easy-to-use API for the MaxMind GeoIP2 andGeoLite2 databases; this package does not support GeoIP Legacy databases.
The structs provided by this package match the internal structure ofthe data in the MaxMind databases.
See github.com/oschwald/maxminddb-golang for more advanced used cases.
Example¶
Example provides a basic example of using the API. Use of the Countrymethod is analogous to that of the City method.
db, err := Open("test-data/test-data/GeoIP2-City-Test.mmdb")if err != nil {log.Panic(err)}defer db.Close()// If you are using strings that may be invalid, check that ip is not nilip := net.ParseIP("81.2.69.142")record, err := db.City(ip)if err != nil {log.Panic(err)}fmt.Printf("Portuguese (BR) city name: %v\n", record.City.Names["pt-BR"])fmt.Printf("English subdivision name: %v\n", record.Subdivisions[0].Names["en"])fmt.Printf("Russian country name: %v\n", record.Country.Names["ru"])fmt.Printf("ISO country code: %v\n", record.Country.IsoCode)fmt.Printf("Time zone: %v\n", record.Location.TimeZone)fmt.Printf("Coordinates: %v, %v\n", record.Location.Latitude, record.Location.Longitude)Output:Portuguese (BR) city name: LondresEnglish subdivision name: EnglandRussian country name: ВеликобританияISO country code: GBTime zone: Europe/LondonCoordinates: 51.5142, -0.0931
Index¶
- type ASN
- type AnonymousIP
- type City
- type ConnectionType
- type Country
- type Domain
- type Enterprise
- type ISP
- type InvalidMethodError
- type Reader
- func (r *Reader) ASN(ipAddress net.IP) (*ASN, error)
- func (r *Reader) AnonymousIP(ipAddress net.IP) (*AnonymousIP, error)
- func (r *Reader) City(ipAddress net.IP) (*City, error)
- func (r *Reader) Close() error
- func (r *Reader) ConnectionType(ipAddress net.IP) (*ConnectionType, error)
- func (r *Reader) Country(ipAddress net.IP) (*Country, error)
- func (r *Reader) Domain(ipAddress net.IP) (*Domain, error)
- func (r *Reader) Enterprise(ipAddress net.IP) (*Enterprise, error)
- func (r *Reader) ISP(ipAddress net.IP) (*ISP, error)
- func (r *Reader) Metadata() maxminddb.Metadata
- type UnknownDatabaseTypeError
Examples¶
Constants¶
This section is empty.
Variables¶
This section is empty.
Functions¶
This section is empty.
Types¶
typeASN¶added inv1.1.0
type ASN struct {AutonomousSystemOrganizationstring `maxminddb:"autonomous_system_organization"`AutonomousSystemNumberuint `maxminddb:"autonomous_system_number"`}The ASN struct corresponds to the data in the GeoLite2 ASN database.
typeAnonymousIP¶added inv1.0.0
type AnonymousIP struct {IsAnonymousbool `maxminddb:"is_anonymous"`IsAnonymousVPNbool `maxminddb:"is_anonymous_vpn"`IsHostingProviderbool `maxminddb:"is_hosting_provider"`IsPublicProxybool `maxminddb:"is_public_proxy"`IsResidentialProxybool `maxminddb:"is_residential_proxy"`IsTorExitNodebool `maxminddb:"is_tor_exit_node"`}The AnonymousIP struct corresponds to the data in the GeoIP2Anonymous IP database.
typeCity¶
type City struct {City struct {Names map[string]string `maxminddb:"names"`GeoNameIDuint `maxminddb:"geoname_id"`} `maxminddb:"city"`Postal struct {Codestring `maxminddb:"code"`} `maxminddb:"postal"`Continent struct {Names map[string]string `maxminddb:"names"`Codestring `maxminddb:"code"`GeoNameIDuint `maxminddb:"geoname_id"`} `maxminddb:"continent"`Subdivisions []struct {Names map[string]string `maxminddb:"names"`IsoCodestring `maxminddb:"iso_code"`GeoNameIDuint `maxminddb:"geoname_id"`} `maxminddb:"subdivisions"`RepresentedCountry struct {Names map[string]string `maxminddb:"names"`IsoCodestring `maxminddb:"iso_code"`Typestring `maxminddb:"type"`GeoNameIDuint `maxminddb:"geoname_id"`IsInEuropeanUnionbool `maxminddb:"is_in_european_union"`} `maxminddb:"represented_country"`Country struct {Names map[string]string `maxminddb:"names"`IsoCodestring `maxminddb:"iso_code"`GeoNameIDuint `maxminddb:"geoname_id"`IsInEuropeanUnionbool `maxminddb:"is_in_european_union"`} `maxminddb:"country"`RegisteredCountry struct {Names map[string]string `maxminddb:"names"`IsoCodestring `maxminddb:"iso_code"`GeoNameIDuint `maxminddb:"geoname_id"`IsInEuropeanUnionbool `maxminddb:"is_in_european_union"`} `maxminddb:"registered_country"`Location struct {TimeZonestring `maxminddb:"time_zone"`Latitudefloat64 `maxminddb:"latitude"`Longitudefloat64 `maxminddb:"longitude"`MetroCodeuint `maxminddb:"metro_code"`AccuracyRadiusuint16 `maxminddb:"accuracy_radius"`} `maxminddb:"location"`Traits struct {IsAnonymousProxybool `maxminddb:"is_anonymous_proxy"`IsAnycastbool `maxminddb:"is_anycast"`IsSatelliteProviderbool `maxminddb:"is_satellite_provider"`} `maxminddb:"traits"`}The City struct corresponds to the data in the GeoIP2/GeoLite2 Citydatabases.
typeConnectionType¶added inv1.0.0
type ConnectionType struct {ConnectionTypestring `maxminddb:"connection_type"`}The ConnectionType struct corresponds to the data in the GeoIP2Connection-Type database.
typeCountry¶
type Country struct {Continent struct {Names map[string]string `maxminddb:"names"`Codestring `maxminddb:"code"`GeoNameIDuint `maxminddb:"geoname_id"`} `maxminddb:"continent"`Country struct {Names map[string]string `maxminddb:"names"`IsoCodestring `maxminddb:"iso_code"`GeoNameIDuint `maxminddb:"geoname_id"`IsInEuropeanUnionbool `maxminddb:"is_in_european_union"`} `maxminddb:"country"`RegisteredCountry struct {Names map[string]string `maxminddb:"names"`IsoCodestring `maxminddb:"iso_code"`GeoNameIDuint `maxminddb:"geoname_id"`IsInEuropeanUnionbool `maxminddb:"is_in_european_union"`} `maxminddb:"registered_country"`RepresentedCountry struct {Names map[string]string `maxminddb:"names"`IsoCodestring `maxminddb:"iso_code"`Typestring `maxminddb:"type"`GeoNameIDuint `maxminddb:"geoname_id"`IsInEuropeanUnionbool `maxminddb:"is_in_european_union"`} `maxminddb:"represented_country"`Traits struct {IsAnonymousProxybool `maxminddb:"is_anonymous_proxy"`IsAnycastbool `maxminddb:"is_anycast"`IsSatelliteProviderbool `maxminddb:"is_satellite_provider"`} `maxminddb:"traits"`}The Country struct corresponds to the data in the GeoIP2/GeoLite2Country databases.
typeDomain¶added inv1.0.0
type Domain struct {Domainstring `maxminddb:"domain"`}The Domain struct corresponds to the data in the GeoIP2 Domain database.
typeEnterprise¶added inv1.3.0
type Enterprise struct {Continent struct {Names map[string]string `maxminddb:"names"`Codestring `maxminddb:"code"`GeoNameIDuint `maxminddb:"geoname_id"`} `maxminddb:"continent"`City struct {Names map[string]string `maxminddb:"names"`GeoNameIDuint `maxminddb:"geoname_id"`Confidenceuint8 `maxminddb:"confidence"`} `maxminddb:"city"`Postal struct {Codestring `maxminddb:"code"`Confidenceuint8 `maxminddb:"confidence"`} `maxminddb:"postal"`Subdivisions []struct {Names map[string]string `maxminddb:"names"`IsoCodestring `maxminddb:"iso_code"`GeoNameIDuint `maxminddb:"geoname_id"`Confidenceuint8 `maxminddb:"confidence"`} `maxminddb:"subdivisions"`RepresentedCountry struct {Names map[string]string `maxminddb:"names"`IsoCodestring `maxminddb:"iso_code"`Typestring `maxminddb:"type"`GeoNameIDuint `maxminddb:"geoname_id"`IsInEuropeanUnionbool `maxminddb:"is_in_european_union"`} `maxminddb:"represented_country"`Country struct {Names map[string]string `maxminddb:"names"`IsoCodestring `maxminddb:"iso_code"`GeoNameIDuint `maxminddb:"geoname_id"`Confidenceuint8 `maxminddb:"confidence"`IsInEuropeanUnionbool `maxminddb:"is_in_european_union"`} `maxminddb:"country"`RegisteredCountry struct {Names map[string]string `maxminddb:"names"`IsoCodestring `maxminddb:"iso_code"`GeoNameIDuint `maxminddb:"geoname_id"`Confidenceuint8 `maxminddb:"confidence"`IsInEuropeanUnionbool `maxminddb:"is_in_european_union"`} `maxminddb:"registered_country"`Traits struct {AutonomousSystemOrganizationstring `maxminddb:"autonomous_system_organization"`ConnectionTypestring `maxminddb:"connection_type"`Domainstring `maxminddb:"domain"`ISPstring `maxminddb:"isp"`MobileCountryCodestring `maxminddb:"mobile_country_code"`MobileNetworkCodestring `maxminddb:"mobile_network_code"`Organizationstring `maxminddb:"organization"`UserTypestring `maxminddb:"user_type"`AutonomousSystemNumberuint `maxminddb:"autonomous_system_number"`StaticIPScorefloat64 `maxminddb:"static_ip_score"`IsAnonymousProxybool `maxminddb:"is_anonymous_proxy"`IsAnycastbool `maxminddb:"is_anycast"`IsLegitimateProxybool `maxminddb:"is_legitimate_proxy"`IsSatelliteProviderbool `maxminddb:"is_satellite_provider"`} `maxminddb:"traits"`Location struct {TimeZonestring `maxminddb:"time_zone"`Latitudefloat64 `maxminddb:"latitude"`Longitudefloat64 `maxminddb:"longitude"`MetroCodeuint `maxminddb:"metro_code"`AccuracyRadiusuint16 `maxminddb:"accuracy_radius"`} `maxminddb:"location"`}The Enterprise struct corresponds to the data in the GeoIP2 Enterprisedatabase.
typeISP¶added inv1.0.0
type ISP struct {AutonomousSystemOrganizationstring `maxminddb:"autonomous_system_organization"`ISPstring `maxminddb:"isp"`MobileCountryCodestring `maxminddb:"mobile_country_code"`MobileNetworkCodestring `maxminddb:"mobile_network_code"`Organizationstring `maxminddb:"organization"`AutonomousSystemNumberuint `maxminddb:"autonomous_system_number"`}The ISP struct corresponds to the data in the GeoIP2 ISP database.
typeInvalidMethodError¶added inv1.0.0
InvalidMethodError is returned when a lookup method is called on adatabase that it does not support. For instance, calling the ISP methodon a City database.
func (InvalidMethodError)Error¶added inv1.0.0
func (eInvalidMethodError) Error()string
typeReader¶
type Reader struct {// contains filtered or unexported fields}Reader holds the maxminddb.Reader struct. It can be created using theOpen and FromBytes functions.
funcFromBytes¶
FromBytes takes a byte slice corresponding to a GeoIP2/GeoLite2 databasefile and returns a Reader struct or an error. Note that the byte slice isused directly; any modification of it after opening the database will resultin errors while reading from the database.
funcOpen¶
Open takes a string path to a file and returns a Reader struct or an error.The database file is opened using a memory map. Use the Close method on theReader object to return the resources to the system.
func (*Reader)ASN¶added inv1.1.0
ASN takes an IP address as a net.IP struct and returns a ASN struct and/oran error.
func (*Reader)AnonymousIP¶added inv1.0.0
func (r *Reader) AnonymousIP(ipAddressnet.IP) (*AnonymousIP,error)
AnonymousIP takes an IP address as a net.IP struct and returns aAnonymousIP struct and/or an error.
func (*Reader)City¶
City takes an IP address as a net.IP struct and returns a City structand/or an error. Although this can be used with other databases, thismethod generally should be used with the GeoIP2 or GeoLite2 City databases.
func (*Reader)Close¶
Close unmaps the database file from virtual memory and returns theresources to the system.
func (*Reader)ConnectionType¶added inv1.0.0
func (r *Reader) ConnectionType(ipAddressnet.IP) (*ConnectionType,error)
ConnectionType takes an IP address as a net.IP struct and returns aConnectionType struct and/or an error.
func (*Reader)Country¶
Country takes an IP address as a net.IP struct and returns a Country structand/or an error. Although this can be used with other databases, thismethod generally should be used with the GeoIP2 or GeoLite2 Countrydatabases.
func (*Reader)Domain¶added inv1.0.0
Domain takes an IP address as a net.IP struct and returns aDomain struct and/or an error.
func (*Reader)Enterprise¶added inv1.3.0
func (r *Reader) Enterprise(ipAddressnet.IP) (*Enterprise,error)
Enterprise takes an IP address as a net.IP struct and returns an Enterprisestruct and/or an error. This is intended to be used with the GeoIP2Enterprise database.
typeUnknownDatabaseTypeError¶added inv1.0.0
type UnknownDatabaseTypeError struct {DatabaseTypestring}UnknownDatabaseTypeError is returned when an unknown database type isopened.
func (UnknownDatabaseTypeError)Error¶added inv1.0.0
func (eUnknownDatabaseTypeError) Error()string