Detecting country

Essential functionality of the IP Geolocation API around country detection

// Let's assume you have saved the API's JSON response to variable `response`
let response = {
    // ... (other fields)
    "locationData": {
        "countryName": "United States",
        "countryCode": "US",
        // ... (other fields)
    }
};

if (response.locationData.countryCode === "US") {
    console.log("A United States visitor from one of the fifty states");
}
package main

import "fmt"

// Assuming you have made the API call and have the JSON response stored in a variable called `response`
var response = map[string]interface{}{
    // ... (other fields)
    "locationData": map[string]interface{}{
        "countryName": "United States",
        "countryCode": "US",
        // ... (other fields)
    },
}

func main() {
    if response["locationData"].(map[string]interface{})["countryCode"] == "US" {
        fmt.Println("A United States visitor from one of the fifty states")
    }
}
response = {
    "locationData": {
        "countryName": "United States",
        "countryCode": "US",
        # ... (other fields)
    }
}

if response['locationData']['countryCode'] == "US":
    print("A United States visitor from one of the fifty states")

Countries of Interest

Finding whether detected country (using ISO2 code) is in a set of countries of interest

let response = {
    "locationData": {
        "countryCode": "US",
    }
};

let fiveEyesCountries = ["US", "GB", "CA", "AU", "NZ"];

let isVisitorFromFiveEyes = fiveEyesCountries.includes(response.locationData.countryCode);

console.log("Is visitor from a 'Five Eyes' country?", isVisitorFromFiveEyes);
package main

import "fmt"

var response = map[string]map[string]string{
	"locationData": {
		"countryCode": "US",
	},
}

var fiveEyesCountries = []string{"US", "GB", "CA", "AU", "NZ"}

func main() {
	isVisitorFromFiveEyes := false
	for _, country := range fiveEyesCountries {
		if response["locationData"]["countryCode"] == country {
			isVisitorFromFiveEyes = true
			break
		}
	}
	fmt.Println("Is visitor from a 'Five Eyes' country?", isVisitorFromFiveEyes)
}
response = {
    "locationData": {
        "countryCode": "US",
    }
}

five_eyes_countries = ["US", "GB", "CA", "AU", "NZ"]

is_visitor_from_five_eyes = response['locationData']['countryCode'] in five_eyes_countries

print("Is visitor from a 'Five Eyes' country?", is_visitor_from_five_eyes)