Libraries

Go SDK

The Go SDK provides a clean and efficient way to integrate GeoIP lookups into your Go applications.


Installation

Install the SDK using go get:

go get github.com/geoipapi/geo-ip-go

Quick Start

package main

import (
    "context"
    "fmt"
    "log"
    
    geoipapigo "github.com/geoipapi/geo-ip-go"
)

func main() {
    ctx := context.Background()
    client := geoipapigo.New()
    
    // Get current IP address
    ipResponse, err := client.GeoIPEndpoints.GetIP(ctx)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Your IP: %s\n", ipResponse.Res.IP)
    
    // Get detailed geolocation data
    geoData, err := client.GeoIPEndpoints.GetIPData(ctx, nil)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Location: %s, %s\n", geoData.Res.Country.City, geoData.Res.Country.Name)
}

Configuration

Create a client with custom configuration:

import "github.com/geoipapi/geo-ip-go/retry"

client := geoipapigo.New(
    geoipapigo.WithServerURL("https://api.geoipapi.com"),
    geoipapigo.WithRetryConfig(retry.Config{
        Strategy: "backoff",
        Backoff: &retry.BackoffStrategy{
            InitialInterval: 1,
            MaxInterval:     50,
            Exponent:        1.1,
            MaxElapsedTime:  100,
        },
    }),
)

Available Methods

GetIP()

Get your current public IP address:

ipResponse, err := client.GeoIPEndpoints.GetIP(ctx)
if err != nil {
    log.Fatal(err)
}
fmt.Printf("Current IP: %s\n", ipResponse.Res.IP)

GetIPData()

Get comprehensive geolocation data:

geoData, err := client.GeoIPEndpoints.GetIPData(ctx, nil)
if err != nil {
    log.Fatal(err)
}

fmt.Printf("Country: %s\n", geoData.Res.Country.Name)
fmt.Printf("City: %s\n", geoData.Res.Country.City)
fmt.Printf("Coordinates: %.6f, %.6f\n", 
    geoData.Res.Location.Latitude, 
    geoData.Res.Location.Longitude)

Response Types

type GeoIPResponse struct {
    IP       string   `json:"ip"`
    Type     string   `json:"type"`
    Country  Country  `json:"country"`
    Location Location `json:"location"`
    ASN      ASN      `json:"asn"`
}

type Country struct {
    IsEUMember   bool   `json:"is_eu_member"`
    CurrencyCode string `json:"currency_code"`
    Continent    string `json:"continent"`
    Name         string `json:"name"`
    CountryCode  string `json:"country_code"`
    State        string `json:"state"`
    City         string `json:"city"`
    Zip          string `json:"zip"`
    Timezone     string `json:"timezone"`
}

type Location struct {
    Latitude  float64 `json:"latitude"`
    Longitude float64 `json:"longitude"`
}

type ASN struct {
    Number  int    `json:"number"`
    Name    string `json:"name"`
    Network string `json:"network"`
    Type    string `json:"type"`
}

Error Handling

Handle API errors with typed errors:

import (
    "errors"
    "github.com/geoipapi/geo-ip-go/models/apierrors"
)

response, err := client.GeoIPEndpoints.GetIPData(ctx, nil)
if err != nil {
    var validationErr *apierrors.HTTPValidationError
    if errors.As(err, &validationErr) {
        fmt.Printf("Validation error: %s\n", validationErr.Error())
        return
    }
    
    var apiErr *apierrors.APIError
    if errors.As(err, &apiErr) {
        fmt.Printf("API error: %s\n", apiErr.Error())
        return
    }
    
    log.Fatal(err)
}

Context Support

The SDK supports Go's context for cancellation and timeouts:

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

result, err := client.GeoIPEndpoints.GetIPData(ctx, nil)
if err != nil {
    log.Fatal(err)
}
Previous
Python
Next
Java