Go Requires Chilkat v11.0.0+
Go
Look up an IP location with IPinfo Core
See more Geolocation Examples
Use an IPinfo token with Core, Plus, or Max access. The current api.ipinfo.io/lookup/ endpoint returns location fields inside geo and network fields inside as. Authenticate with a Bearer token.
IPinfo continues to support its older ipinfo.io API for existing integrations. This example uses the current Core response structure. The free Lite API uses /lite/ and a different, country-level response. Choose the endpoint and parser for your subscription.
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")
http.SetAuthToken("IPINFO_TOKEN")
url := chilkat.NewStringBuilder()
url.Append("https://api.ipinfo.io/lookup/")
url.Append(*http.UrlEncode(ipAddress))
response := chilkat.NewHttpResponse()
success = http.HttpNoBody("GET",*url.GetAsString(),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()
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("error.title") == 1 {
fmt.Println(*json.StringOf("error.title"))
}
if json.JsonTypeOf("error.message") == 1 {
fmt.Println(*json.StringOf("error.message"))
}
}
http.DisposeHttp()
url.DisposeStringBuilder()
response.DisposeHttpResponse()
json.DisposeJsonObject()
return
}
if parsed != true {
fmt.Println("Expected a JSON object in the lookup response.")
http.DisposeHttp()
url.DisposeStringBuilder()
response.DisposeHttpResponse()
json.DisposeJsonObject()
return
}
if (json.JsonTypeOf("error") == 3) {
fmt.Println("The service could not complete the lookup.")
if json.JsonTypeOf("error.title") == 1 {
fmt.Println(*json.StringOf("error.title"))
}
if json.JsonTypeOf("error.message") == 1 {
fmt.Println(*json.StringOf("error.message"))
}
http.DisposeHttp()
url.DisposeStringBuilder()
response.DisposeHttpResponse()
json.DisposeJsonObject()
return
}
if (json.JsonTypeOf("bogon") == 5) && (json.BoolOf("bogon") == true) {
fmt.Println("This is a reserved or otherwise non-public IP address.")
http.DisposeHttp()
url.DisposeStringBuilder()
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()
response.DisposeHttpResponse()
json.DisposeJsonObject()
return
}
fmt.Println("IP address: ", *json.StringOf("ip"))
if (json.JsonTypeOf("geo.country") == 1) && (json.StringOfEquals("geo.country","",true) != true) {
fmt.Println("Country: ", *json.StringOf("geo.country"))
} else {
fmt.Println("Country: unavailable")
}
if (json.JsonTypeOf("geo.country_code") == 1) && (json.StringOfEquals("geo.country_code","",true) != true) {
fmt.Println("Country code: ", *json.StringOf("geo.country_code"))
} else {
fmt.Println("Country code: unavailable")
}
if (json.JsonTypeOf("geo.region") == 1) && (json.StringOfEquals("geo.region","",true) != true) {
fmt.Println("Region: ", *json.StringOf("geo.region"))
} else {
fmt.Println("Region: unavailable")
}
if (json.JsonTypeOf("geo.city") == 1) && (json.StringOfEquals("geo.city","",true) != true) {
fmt.Println("City: ", *json.StringOf("geo.city"))
} else {
fmt.Println("City: unavailable")
}
if (json.JsonTypeOf("geo.timezone") == 1) && (json.StringOfEquals("geo.timezone","",true) != true) {
fmt.Println("Time zone: ", *json.StringOf("geo.timezone"))
} else {
fmt.Println("Time zone: unavailable")
}
// Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
if (json.JsonTypeOf("geo.latitude") == 2) && (json.JsonTypeOf("geo.longitude") == 2) {
fmt.Println("Coordinates: ", *json.StringOf("geo.latitude"), ", ", *json.StringOf("geo.longitude"))
} else {
fmt.Println("Coordinates: unavailable")
}
if (json.JsonTypeOf("as.asn") == 1) && (json.StringOfEquals("as.asn","",true) != true) {
fmt.Println("ASN: ", *json.StringOf("as.asn"))
} else {
fmt.Println("ASN: unavailable")
}
if (json.JsonTypeOf("as.name") == 1) && (json.StringOfEquals("as.name","",true) != true) {
fmt.Println("Network organization: ", *json.StringOf("as.name"))
} else {
fmt.Println("Network organization: unavailable")
}
http.DisposeHttp()
url.DisposeStringBuilder()
response.DisposeHttpResponse()
json.DisposeJsonObject()