Movatterモバイル変換


[0]ホーム

URL:


Notice  The highest tagged major version isv2.

geoip2

packagemodule
v1.13.0Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2025 License:ISCImports:3Imported by:1,028

Details

Repository

github.com/oschwald/geoip2-golang

Links

README

GeoIP2 Reader for Go

PkgGoDev

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-golang

Usage

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 update

Execute test suite:

go test

Contributing

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

typeASNadded 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.

typeAnonymousIPadded 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.

typeConnectionTypeadded 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.

typeDomainadded inv1.0.0

type Domain struct {Domainstring `maxminddb:"domain"`}

The Domain struct corresponds to the data in the GeoIP2 Domain database.

typeEnterpriseadded 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.

typeISPadded 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.

typeInvalidMethodErroradded inv1.0.0

type InvalidMethodError struct {MethodstringDatabaseTypestring}

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)Erroradded 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

func FromBytes(bytes []byte) (*Reader,error)

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

func Open(filestring) (*Reader,error)

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)ASNadded inv1.1.0

func (r *Reader) ASN(ipAddressnet.IP) (*ASN,error)

ASN takes an IP address as a net.IP struct and returns a ASN struct and/oran error.

func (*Reader)AnonymousIPadded 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

func (r *Reader) City(ipAddressnet.IP) (*City,error)

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

func (r *Reader) Close()error

Close unmaps the database file from virtual memory and returns theresources to the system.

func (*Reader)ConnectionTypeadded 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

func (r *Reader) Country(ipAddressnet.IP) (*Country,error)

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)Domainadded inv1.0.0

func (r *Reader) Domain(ipAddressnet.IP) (*Domain,error)

Domain takes an IP address as a net.IP struct and returns aDomain struct and/or an error.

func (*Reader)Enterpriseadded 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.

func (*Reader)ISPadded inv1.0.0

func (r *Reader) ISP(ipAddressnet.IP) (*ISP,error)

ISP takes an IP address as a net.IP struct and returns a ISP struct and/oran error.

func (*Reader)Metadataadded inv1.0.0

func (r *Reader) Metadata() maxminddb.Metadata

Metadata takes no arguments and returns a struct containing metadata aboutthe MaxMind database in use by the Reader.

typeUnknownDatabaseTypeErroradded inv1.0.0

type UnknownDatabaseTypeError struct {DatabaseTypestring}

UnknownDatabaseTypeError is returned when an unknown database type isopened.

func (UnknownDatabaseTypeError)Erroradded inv1.0.0

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp