Integrating IP2Location C Library With Go Programming Language

The aim of this guide is to demonstrate integrating the IP2Location C library within the Go programming language. In our example, we will be using the Linux environment.

First of all, you will need to install the IP2Location C library.
Follow the instructions at https://ip2location.com/developers/c to install the IP2Location C library.

Next, install Go on your Linux.
Follow the instructions at https://go.dev/doc/install to install the Go tools.

Next, you will need to download the IP2Location BIN file.

Extract out the BIN file and copy to the folder where you will store your Go codes.

Creating the Go codes

Create a text file called IP2Location.go and paste the following codes into it. Our example uses the DB24 BIN file.

The code does the following:

  1. Dynamically loads the IP2Location C library for use in the Go codes.
  2. Initializing the IP2Location object with the specified BIN database path.
  3. Performing geolocation query for the specified IP address and displaying the results.
  4. Performs cleanup and unload the IP2Location C library.

NOTE: Modify the db variable to store your own path to your BIN database file. Modify the ipAddress variable to store the IP address you wish to query.

package main
/*
#include <IP2Location.h>
#cgo LDFLAGS: -L. -lIP2Location
*/
import "C"
import "fmt"
import "os"
 
func main() {
    // the path to the IP2Location BIN database (modify this to your own path)
    db := "./IPV6-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE.BIN"
 
    // IP address to query (modify this to the IP address you wish to query)
    ipAddress := "8.8.8.8"
 
    // initializing the IP2Location object with BIN database path
    IP2LocationObj := C.IP2Location_open(C.CString(db))
    if IP2LocationObj == nil {
        fmt.Printf("Please install the database in the correct path.\n")
        os.Exit(1)
    }
 
    // uncomment this section if you want to load BIN database into cached memory
    // if C.IP2Location_open_mem(IP2LocationObj, C.IP2LOCATION_CACHE_MEMORY) == -1 {
        // fmt.Printf("Unable to load data into cache memory.\n")
        // os.Exit(1)
    // }
 
    // uncomment this section if you want to load BIN database into shared memory
    // if C.IP2Location_open_mem(IP2LocationObj, C.IP2LOCATION_SHARED_MEMORY) == -1 {
        // fmt.Printf("Unable to load data into shared memory.\n")
        // os.Exit(1)
    // }
 
    // performs IP2Location geolocation query for that specified IP address
    record := C.IP2Location_get_all(IP2LocationObj, C.CString(ipAddress))
 
    // display results if IP address is found in the BIN database
    if record == nil {
        fmt.Printf("Unable to locate IP address in the current database.\n")
    } else {
        fmt.Printf("IP Address: %s\n", ipAddress)
        fmt.Printf("Country Code: %s\n", C.GoString(record.country_short))
        fmt.Printf("Country Name: %s\n", C.GoString(record.country_long))
        fmt.Printf("Region: %s\n", C.GoString(record.region))
        fmt.Printf("City: %s\n", C.GoString(record.city))
        fmt.Printf("ISP: %s\n", C.GoString(record.isp))
        fmt.Printf("Latitude: %f\n", record.latitude)
        fmt.Printf("Longitude: %f\n", record.longitude)
        fmt.Printf("Domain: %s\n", C.GoString(record.domain))
        fmt.Printf("ZIP Code: %s\n", C.GoString(record.zipcode))
        fmt.Printf("Timezone: %s\n", C.GoString(record.timezone))
        fmt.Printf("Netspeed: %s\n", C.GoString(record.netspeed))
        fmt.Printf("IDD Code: %s\n", C.GoString(record.iddcode))
        fmt.Printf("Area Code: %s\n", C.GoString(record.areacode))
        fmt.Printf("Weather Station Code: %s\n", C.GoString(record.weatherstationcode))
        fmt.Printf("Weather Station Name: %s\n", C.GoString(record.weatherstationname))
        fmt.Printf("MCC: %s\n", C.GoString(record.mcc))
        fmt.Printf("MNC: %s\n", C.GoString(record.mnc))
        fmt.Printf("Mobile Brand: %s\n", C.GoString(record.mobilebrand))
        fmt.Printf("Elevation: %f\n", record.elevation)
        fmt.Printf("Usage Type: %s\n", C.GoString(record.usagetype))
        fmt.Printf("API Version: %s\n", C.GoString(C.IP2Location_api_version_string()))
    }
 
    // free any allocated memory
    C.IP2Location_free_record(record)
 
    // close BIN database file handle
    C.IP2Location_close(IP2LocationObj)
 
    // delete shared memory if it was used
    C.IP2Location_delete_shm()
}

Compiling the IP2Location.go

At the command prompt, run the following command:

go build IP2Location.go

Run the program

At the command prompt, run the following command:

./IP2Location

You should see something like below if everything goes well.

Go Language

Was this article helpful?

Related Articles