### Install GeoIP2 Reader for Go Source: https://github.com/oschwald/geoip2-golang/blob/main/README.md Use this command to install the GeoIP2 Reader library for Go. Ensure you have Go installed and configured. ```bash go get github.com/oschwald/geoip2-golang/v2 ``` -------------------------------- ### Migrate GeoIP2 City Lookup from v1 to v2 in Go Source: https://github.com/oschwald/geoip2-golang/blob/main/README.md This example demonstrates the changes required to migrate a city lookup from geoip2-golang v1 to v2, including import paths, IP address types, and accessing city names. ```go // v1 import "github.com/oschwald/geoip2-golang" ip := net.ParseIP("81.2.69.142") record, err := db.City(ip) cityName := record.City.Names["en"] // v2 import "github.com/oschwald/geoip2-golang/v2" ip, err := netip.ParseAddr("81.2.69.142") if err != nil { // handle error } record, err := db.City(ip) if !record.HasData() { // handle no data found } cityName := record.City.Names.English ``` -------------------------------- ### Execute Go Test Suite Source: https://github.com/oschwald/geoip2-golang/blob/main/README.md Run the library's test suite to verify its functionality. ```bash go test ``` -------------------------------- ### Query City Database with Error Handling Source: https://github.com/oschwald/geoip2-golang/blob/main/README.md Opens a City database and queries it for a private IP address. Demonstrates comprehensive error handling for database operations and IP parsing. Includes checks for data availability and individual field existence before accessing them. ```go package main import ( "fmt" "log" "net/netip" "github.com/oschwald/geoip2-golang/v2" ) func main() { db, err := geoip2.Open("GeoIP2-City.mmdb") if err != nil { log.Fatal(err) } defer db.Close() ip, err := netip.ParseAddr("10.0.0.1") // Private IP if err != nil { log.Fatal(err) } record, err := db.City(ip) if err != nil { log.Fatal(err) } // Always check if data was found if !record.HasData() { fmt.Println("No data found for this IP address") return } // Check individual fields before using them if record.City.Names.English != "" { fmt.Printf("City: %v\n", record.City.Names.English) } else { fmt.Println("City name not available") } // Check array bounds for subdivisions if len(record.Subdivisions) > 0 { fmt.Printf("Subdivision: %v\n", record.Subdivisions[0].Names.English) } else { fmt.Println("No subdivision data available") } fmt.Printf("Country: %v\n", record.Country.Names.English) } ``` -------------------------------- ### Reuse Database Instance in Go Source: https://github.com/oschwald/geoip2-golang/blob/main/README.md Always reuse database instances across requests for better performance. Ensure the database is closed when no longer needed. ```go // Good: Create once, use many times db, err := geoip2.Open("GeoIP2-City.mmdb") if err != nil { log.Fatal(err) } deferr db.Close() // Use db for multiple lookups... ``` -------------------------------- ### Initialize and Update Git Submodules Source: https://github.com/oschwald/geoip2-golang/blob/main/README.md Before running tests, ensure that the test data submodule is properly initialized and updated. ```bash git submodule init git submodule update ``` -------------------------------- ### Switch IP Address Type from net.IP to netip.Addr Source: https://github.com/oschwald/geoip2-golang/blob/main/MIGRATION.md Update lookup methods to use `netip.Addr` instead of `net.IP`. Use `netip.ParseAddr` for parsing and error handling, or `netip.MustParseAddr` for trusted literals. ```go // v1 ip := net.ParseIP("81.2.69.142") record, err := db.City(ip) // v2 ip, err := netip.ParseAddr("81.2.69.142") if err != nil { return err } record, err := db.City(ip) ``` -------------------------------- ### Query Enterprise Database in Go Source: https://github.com/oschwald/geoip2-golang/blob/main/README.md Use this snippet to retrieve comprehensive location and enterprise data. Ensure you have the GeoIP2-Enterprise.mmdb file. ```go package main import ( "fmt" "log" "net/netip" "github.com/oschwald/geoip2-golang/v2" ) func main() { db, err := geoip2.Open("GeoIP2-Enterprise.mmdb") if err != nil { log.Fatal(err) } defer db.Close() ip, err := netip.ParseAddr("128.101.101.101") if err != nil { log.Fatal(err) } record, err := db.Enterprise(ip) if err != nil { log.Fatal(err) } if !record.HasData() { fmt.Println("No data found for this IP") return } // Basic location information fmt.Printf("City: %v\n", record.City.Names.English) fmt.Printf("Country: %v (%v)\n", record.Country.Names.English, record.Country.ISOCode) if record.Location.HasCoordinates() { fmt.Printf("Location: %v, %v\n", *record.Location.Latitude, *record.Location.Longitude) } // Enterprise-specific fields fmt.Printf("ISP: %v\n", record.Traits.ISP) fmt.Printf("Organization: %v\n", record.Traits.Organization) fmt.Printf("ASN: %v (%v)\n", record.Traits.AutonomousSystemNumber, record.Traits.AutonomousSystemOrganization) fmt.Printf("Connection Type: %v\n", record.Traits.ConnectionType) fmt.Printf("Domain: %v\n", record.Traits.Domain) fmt.Printf("User Type: %v\n", record.Traits.UserType) fmt.Printf("Is Anycast: %v\n", record.Traits.IsAnycast) // Mobile carrier information (if available) if record.Traits.MobileCountryCode != "" { fmt.Printf("Mobile Country Code: %v\n", record.Traits.MobileCountryCode) fmt.Printf("Mobile Network Code: %v\n", record.Traits.MobileNetworkCode) } fmt.Printf("Network: %v\n", record.Traits.Network) fmt.Printf("IP Address: %v\n", record.Traits.IPAddress) } ``` -------------------------------- ### Query IP Address City Information Source: https://github.com/oschwald/geoip2-golang/blob/main/README.md Demonstrates how to open a GeoIP2 database, parse an IP address, and retrieve detailed city, subdivision, country, and location information. Ensure you have the 'GeoIP2-City.mmdb' file available in the same directory or provide the correct path. Handles potential errors during database opening, IP parsing, and data retrieval. ```go package main import ( "fmt" "log" "net/netip" "github.com/oschwald/geoip2-golang/v2" ) 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, use netip.ParseAddr and check for errors ip, err := netip.ParseAddr("81.2.69.142") if err != nil { log.Fatal(err) } record, err := db.City(ip) if err != nil { log.Fatal(err) } if !record.HasData() { fmt.Println("No data found for this IP") return } fmt.Printf("Portuguese (BR) city name: %v\n", record.City.Names.BrazilianPortuguese) if len(record.Subdivisions) > 0 { fmt.Printf("English subdivision name: %v\n", record.Subdivisions[0].Names.English) } fmt.Printf("Russian country name: %v\n", record.Country.Names.Russian) fmt.Printf("ISO country code: %v\n", record.Country.ISOCode) fmt.Printf("Time zone: %v\n", record.Location.TimeZone) if record.Location.HasCoordinates() { 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 } ``` -------------------------------- ### Query ISP Database in Go Source: https://github.com/oschwald/geoip2-golang/blob/main/README.md Use this snippet to retrieve ISP, organization, and ASN information. Ensure you have the GeoIP2-ISP.mmdb file. ```go package main import ( "fmt" "log" "net/netip" "github.com/oschwald/geoip2-golang/v2" ) func main() { db, err := geoip2.Open("GeoIP2-ISP.mmdb") if err != nil { log.Fatal(err) } defer db.Close() ip, err := netip.ParseAddr("1.128.0.0") if err != nil { log.Fatal(err) } record, err := db.ISP(ip) if err != nil { log.Fatal(err) } if !record.HasData() { fmt.Println("No data found for this IP") return } fmt.Printf("ISP: %v\n", record.ISP) fmt.Printf("Organization: %v\n", record.Organization) fmt.Printf("ASN: %v (%v)\n", record.AutonomousSystemNumber, record.AutonomousSystemOrganization) // Mobile carrier information (if available) if record.MobileCountryCode != "" { fmt.Printf("Mobile Country Code: %v\n", record.MobileCountryCode) fmt.Printf("Mobile Network Code: %v\n", record.MobileNetworkCode) } fmt.Printf("Network: %v\n", record.Network) fmt.Printf("IP Address: %v\n", record.IPAddress) } ``` -------------------------------- ### Query Domain Database in Go Source: https://github.com/oschwald/geoip2-golang/blob/main/README.md Use this snippet to retrieve the second-level domain associated with an IP address. Ensure you have the GeoIP2-Domain.mmdb file. ```go package main import ( "fmt" "log" "net/netip" "github.com/oschwald/geoip2-golang/v2" ) func main() { db, err := geoip2.Open("GeoIP2-Domain.mmdb") if err != nil { log.Fatal(err) } defer db.Close() ip, err := netip.ParseAddr("1.2.0.0") if err != nil { log.Fatal(err) } record, err := db.Domain(ip) if err != nil { log.Fatal(err) } if !record.HasData() { fmt.Println("No data found for this IP") return } fmt.Printf("Domain: %v\n", record.Domain) fmt.Printf("Network: %v\n", record.Network) fmt.Printf("IP Address: %v\n", record.IPAddress) } ``` -------------------------------- ### Query Connection Type Database Source: https://github.com/oschwald/geoip2-golang/blob/main/README.md Opens a Connection Type database and queries it for a given IP address. Handles potential errors during database opening and IP parsing. Checks if data exists before printing results. ```go package main import ( "fmt" "log" "net/netip" "github.com/oschwald/geoip2-golang/v2" ) func main() { db, err := geoip2.Open("GeoIP2-Connection-Type.mmdb") if err != nil { log.Fatal(err) } defer db.Close() ip, err := netip.ParseAddr("1.0.128.0") if err != nil { log.Fatal(err) } record, err := db.ConnectionType(ip) if err != nil { log.Fatal(err) } if !record.HasData() { fmt.Println("No data found for this IP") return } fmt.Printf("Connection Type: %v\n", record.ConnectionType) fmt.Printf("Network: %v\n", record.Network) fmt.Printf("IP Address: %v\n", record.IPAddress) } ``` -------------------------------- ### Query City Database in Go Source: https://github.com/oschwald/geoip2-golang/blob/main/README.md Opens the GeoIP2-City.mmdb database and retrieves detailed city, subdivision, country, continent, postal code, location, time zone, network, and IP address information for a given IP address. Ensure the GeoIP2-City.mmdb file is accessible. ```go package main import ( "fmt" "log" "net/netip" "github.com/oschwald/geoip2-golang/v2" ) func main() { db, err := geoip2.Open("GeoIP2-City.mmdb") if err != nil { log.Fatal(err) } defer db.Close() ip, err := netip.ParseAddr("128.101.101.101") if err != nil { log.Fatal(err) } record, err := db.City(ip) if err != nil { log.Fatal(err) } if !record.HasData() { fmt.Println("No data found for this IP") return } fmt.Printf("City: %v\n", record.City.Names.English) fmt.Printf("Subdivision: %v\n", record.Subdivisions[0].Names.English) fmt.Printf("Country: %v (%v)\n", record.Country.Names.English, record.Country.ISOCode) fmt.Printf("Continent: %v (%v)\n", record.Continent.Names.English, record.Continent.Code) fmt.Printf("Postal Code: %v\n", record.Postal.Code) if record.Location.HasCoordinates() { fmt.Printf("Location: %v, %v\n", *record.Location.Latitude, *record.Location.Longitude) } fmt.Printf("Time Zone: %v\n", record.Location.TimeZone) fmt.Printf("Network: %v\n", record.Traits.Network) fmt.Printf("IP Address: %v\n", record.Traits.IPAddress) } ``` -------------------------------- ### Update Go Imports for geoip2-golang Source: https://github.com/oschwald/geoip2-golang/blob/main/MIGRATION.md Change the import path from the v1 to the v2 version of the geoip2-golang library. ```go import "github.com/oschwald/geoip2-golang" // v2 import "github.com/oschwald/geoip2-golang/v2" ``` -------------------------------- ### Query ASN Database in Go Source: https://github.com/oschwald/geoip2-golang/blob/main/README.md Opens the GeoLite2-ASN.mmdb database and retrieves Autonomous System Number (ASN) and organization information for a given IP address. Ensure the GeoLite2-ASN.mmdb file is accessible. ```go package main import ( "fmt" "log" "net/netip" "github.com/oschwald/geoip2-golang/v2" ) func main() { db, err := geoip2.Open("GeoLite2-ASN.mmdb") if err != nil { log.Fatal(err) } defer db.Close() ip, err := netip.ParseAddr("1.128.0.0") if err != nil { log.Fatal(err) } record, err := db.ASN(ip) if err != nil { log.Fatal(err) } if !record.HasData() { fmt.Println("No data found for this IP") return } fmt.Printf("ASN: %v\n", record.AutonomousSystemNumber) fmt.Printf("Organization: %v\n", record.AutonomousSystemOrganization) fmt.Printf("Network: %v\n", record.Network) fmt.Printf("IP Address: %v\n", record.IPAddress) } ``` -------------------------------- ### Query Country Database in Go Source: https://github.com/oschwald/geoip2-golang/blob/main/README.md Opens the GeoIP2-Country.mmdb database and retrieves country-level geolocation data, including country, continent, EU membership, network, IP address, and registered country information for a given IP address. Ensure the GeoIP2-Country.mmdb file is accessible. ```go package main import ( "fmt" "log" "net/netip" "github.com/oschwald/geoip2-golang/v2" ) func main() { db, err := geoip2.Open("GeoIP2-Country.mmdb") if err != nil { log.Fatal(err) } defer db.Close() ip, err := netip.ParseAddr("81.2.69.142") if err != nil { log.Fatal(err) } record, err := db.Country(ip) if err != nil { log.Fatal(err) } if !record.HasData() { fmt.Println("No data found for this IP") return } fmt.Printf("Country: %v (%v)\n", record.Country.Names.English, record.Country.ISOCode) fmt.Printf("Continent: %v (%v)\n", record.Continent.Names.English, record.Continent.Code) fmt.Printf("Is in EU: %v\n", record.Country.IsInEuropeanUnion) fmt.Printf("Network: %v\n", record.Traits.Network) fmt.Printf("IP Address: %v\n", record.Traits.IPAddress) if record.RegisteredCountry.Names.English != "" { fmt.Printf("Registered Country: %v (%v)\n", record.RegisteredCountry.Names.English, record.RegisteredCountry.ISOCode) } } ``` -------------------------------- ### Query Anonymous Plus Database in Go Source: https://github.com/oschwald/geoip2-golang/blob/main/README.md Use this snippet to retrieve anonymous IP information, including confidence scores and provider details. Ensure you have the GeoIP-Anonymous-Plus.mmdb file. ```go package main import ( "fmt" "log" "net/netip" "github.com/oschwald/geoip2-golang/v2" ) func main() { db, err := geoip2.Open("GeoIP-Anonymous-Plus.mmdb") if err != nil { log.Fatal(err) } defer db.Close() ip, err := netip.ParseAddr("1.2.0.1") if err != nil { log.Fatal(err) } record, err := db.AnonymousPlus(ip) if err != nil { log.Fatal(err) } if !record.HasData() { fmt.Println("No data found for this IP") return } // Standard anonymous IP flags fmt.Printf("Is Anonymous: %v\n", record.IsAnonymous) fmt.Printf("Is Anonymous VPN: %v\n", record.IsAnonymousVPN) fmt.Printf("Is Hosting Provider: %v\n", record.IsHostingProvider) fmt.Printf("Is Public Proxy: %v\n", record.IsPublicProxy) fmt.Printf("Is Residential Proxy: %v\n", record.IsResidentialProxy) fmt.Printf("Is Tor Exit Node: %v\n", record.IsTorExitNode) // Anonymous Plus specific fields fmt.Printf("Anonymizer Confidence: %v\n", record.AnonymizerConfidence) fmt.Printf("Provider Name: %v\n", record.ProviderName) if !record.NetworkLastSeen.IsZero() { fmt.Printf("Network Last Seen: %v\n", record.NetworkLastSeen.Format("2006-01-02")) } fmt.Printf("Network: %v\n", record.Network) fmt.Printf("IP Address: %v\n", record.IPAddress) } ``` -------------------------------- ### Use HasData() for GeoIP Record Checks Source: https://github.com/oschwald/geoip2-golang/blob/main/MIGRATION.md Utilize the `HasData()` method on result structs to check for the presence of GeoIP data, ignoring the `Network` and `IPAddress` fields. This replaces checks against zero values. ```go record, err := db.City(ip) if err != nil { return err } if !record.HasData() { // handle missing GeoIP data } ``` -------------------------------- ### Query Anonymous IP Database in Go Source: https://github.com/oschwald/geoip2-golang/blob/main/README.md Opens the GeoIP2-Anonymous-IP.mmdb database and checks if an IP address is associated with various types of anonymous networks, such as VPNs, public proxies, or Tor exit nodes. Ensure the GeoIP2-Anonymous-IP.mmdb file is accessible. ```go package main import ( "fmt" "log" "net/netip" "github.com/oschwald/geoip2-golang/v2" ) func main() { db, err := geoip2.Open("GeoIP2-Anonymous-IP.mmdb") if err != nil { log.Fatal(err) } defer db.Close() ip, err := netip.ParseAddr("81.2.69.142") if err != nil { log.Fatal(err) } record, err := db.AnonymousIP(ip) if err != nil { log.Fatal(err) } if !record.HasData() { fmt.Println("No data found for this IP") return } fmt.Printf("Is Anonymous: %v\n", record.IsAnonymous) fmt.Printf("Is Anonymous VPN: %v\n", record.IsAnonymousVPN) fmt.Printf("Is Hosting Provider: %v\n", record.IsHostingProvider) fmt.Printf("Is Public Proxy: %v\n", record.IsPublicProxy) fmt.Printf("Is Residential Proxy: %v\n", record.IsResidentialProxy) fmt.Printf("Is Tor Exit Node: %v\n", record.IsTorExitNode) fmt.Printf("Network: %v\n", record.Network) fmt.Printf("IP Address: %v\n", record.IPAddress) } ``` -------------------------------- ### Access Localized Names with Structured Fields Source: https://github.com/oschwald/geoip2-golang/blob/main/MIGRATION.md Access localized names directly through strongly typed struct fields (e.g., `Names.English`) instead of map lookups. ```go // v1 cityEn := record.City.Names["en"] cityPtBr := record.City.Names["pt-BR"] // v2 cityEn := record.City.Names.English cityPtBr := record.City.Names.BrazilianPortuguese ``` -------------------------------- ### Update IsoCode Field to ISOCode Source: https://github.com/oschwald/geoip2-golang/blob/main/MIGRATION.md Rename the `IsoCode` field to `ISOCode` in all result structs to maintain consistency. ```go // v1 code := record.Country.IsoCode // v2 code := record.Country.ISOCode ``` -------------------------------- ### JSON Serialization of GeoIP2 Records in Go Source: https://github.com/oschwald/geoip2-golang/blob/main/README.md All result structs in geoip2-golang support marshaling to JSON. This snippet shows how to serialize a City record to a JSON string. ```go record, err := db.City(ip) if err != nil { log.Fatal(err) } jsonData, err := json.Marshal(record) if err != nil { log.Fatal(err) } fmt.Println(string(jsonData)) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.