package main import ( "encoding/json" "flag" "fmt" "net/http" ) type Status struct { IP string `json:"ip"` Hostname string `json:"hostname"` City string `json:"city"` Region string `json:"region"` Country string `json:"country"` Location string `json:"loc"` Organization string `json:"org"` Postal string `json:"postal"` Timezone string `json:"timezone"` } const ( url = "https://ipinfo.io" ) var ( ip_addr string ) func init() { flag.StringVar(&ip_addr, "i", "1.1.1.1", "IP address to check") } func main() { flag.Parse() if len(ip_addr) == 0 { fmt.Println("No IP address given, trying default") } var status Status query_url := url + "/" + ip_addr resp, err := http.Get(query_url) if err != nil { fmt.Println(err) } defer resp.Body.Close() _ = json.NewDecoder(resp.Body).Decode(&status) fmt.Printf("IP: %v\nHostname: %v\nCity: %v\nRegion: %v\nCountry %v\nLocation: %v\nOrganization: %v\nPostal: %v\nTimezone: %v\n", status.IP, status.Hostname, status.City, status.Region, status.Country, status.Location, status.Organization, status.Postal, status.Timezone) }