Go Requires Chilkat v11.0.0+
Go
Look up an IP address with ipdata
See more Geolocation Examples
Use an ipdata API key to request a public IPv4 or IPv6 address. The key is encoded as the api-key query parameter. The example displays location fields and the optional ASN object using its current nested structure. Data access and quotas depend on the account plan.
IP geolocation estimates a network location. VPNs, mobile gateways, and anycast routing can affect the result. City and coordinate fields can be missing or approximate. The sample address 8.8.8.8 is a public DNS address used to demonstrate the request.
Chilkat Go Downloads
success := false
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Use a public IP address. Provider results can change as their databases are updated.
ipAddress := "8.8.8.8"
http := chilkat.NewHttp()
http.SetRequireSslCertVerify(true)
http.SetConnectTimeout(10)
http.SetReadTimeout(30)
http.SetAccept("application/json")
apiKey := "YOUR_API_KEY"
url := chilkat.NewStringBuilder()
url.Append("https://api.ipdata.co/")
url.Append(*http.UrlEncode(ipAddress))
parameters := chilkat.NewJsonObject()
parameters.UpdateString("api-key",apiKey)
response := chilkat.NewHttpResponse()
success = http.HttpParams("GET",*url.GetAsString(),parameters,response)
if response.StatusCode() == 0 {
// Request diagnostics can contain an API key. Report transport failure without logging credentials.
fmt.Println("No HTTP response. Check connectivity, DNS, and TLS configuration.")
http.DisposeHttp()
url.DisposeStringBuilder()
parameters.DisposeJsonObject()
response.DisposeHttpResponse()
return
}
json := chilkat.NewJsonObject()
parsed := json.Load(response.BodyStr())
if (success != true) || (response.StatusCode() < 200) || (response.StatusCode() >= 300) {
fmt.Println("Lookup failed. HTTP ", response.StatusCode())
if parsed == true {
if json.JsonTypeOf("message") == 1 {
fmt.Println(*json.StringOf("message"))
}
}
http.DisposeHttp()
url.DisposeStringBuilder()
parameters.DisposeJsonObject()
response.DisposeHttpResponse()
json.DisposeJsonObject()
return
}
if parsed != true {
fmt.Println("Expected a JSON object in the lookup response.")
http.DisposeHttp()
url.DisposeStringBuilder()
parameters.DisposeJsonObject()
response.DisposeHttpResponse()
json.DisposeJsonObject()
return
}
if (json.JsonTypeOf("ip") != 1) || (json.StringOfEquals("ip","",true) == true) {
fmt.Println("The response did not contain the expected IP address.")
http.DisposeHttp()
url.DisposeStringBuilder()
parameters.DisposeJsonObject()
response.DisposeHttpResponse()
json.DisposeJsonObject()
return
}
fmt.Println("IP address: ", *json.StringOf("ip"))
if (json.JsonTypeOf("country_name") == 1) && (json.StringOfEquals("country_name","",true) != true) {
fmt.Println("Country: ", *json.StringOf("country_name"))
} else {
fmt.Println("Country: unavailable")
}
if (json.JsonTypeOf("country_code") == 1) && (json.StringOfEquals("country_code","",true) != true) {
fmt.Println("Country code: ", *json.StringOf("country_code"))
} else {
fmt.Println("Country code: unavailable")
}
if (json.JsonTypeOf("region") == 1) && (json.StringOfEquals("region","",true) != true) {
fmt.Println("Region: ", *json.StringOf("region"))
} else {
fmt.Println("Region: unavailable")
}
if (json.JsonTypeOf("city") == 1) && (json.StringOfEquals("city","",true) != true) {
fmt.Println("City: ", *json.StringOf("city"))
} else {
fmt.Println("City: unavailable")
}
if (json.JsonTypeOf("time_zone.name") == 1) && (json.StringOfEquals("time_zone.name","",true) != true) {
fmt.Println("Time zone: ", *json.StringOf("time_zone.name"))
} else {
fmt.Println("Time zone: unavailable")
}
// Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
if (json.JsonTypeOf("latitude") == 2) && (json.JsonTypeOf("longitude") == 2) {
fmt.Println("Coordinates: ", *json.StringOf("latitude"), ", ", *json.StringOf("longitude"))
} else {
fmt.Println("Coordinates: unavailable")
}
if (json.JsonTypeOf("asn.asn") == 1) && (json.StringOfEquals("asn.asn","",true) != true) {
fmt.Println("ASN: ", *json.StringOf("asn.asn"))
} else {
fmt.Println("ASN: unavailable")
}
if (json.JsonTypeOf("asn.name") == 1) && (json.StringOfEquals("asn.name","",true) != true) {
fmt.Println("Network organization: ", *json.StringOf("asn.name"))
} else {
fmt.Println("Network organization: unavailable")
}
http.DisposeHttp()
url.DisposeStringBuilder()
parameters.DisposeJsonObject()
response.DisposeHttpResponse()
json.DisposeJsonObject()