How can I get this output in clear text any easy method to parse the data?
func iplocation() {
var ip string
fmt.Print("Enter IP Adress: ")
fmt.Scan(&ip)
lip, err := http.Get("http://ipinfo.io/" + ip + "/geo")
if err != nil {
log.Fatal(err)
os.Exit(0)
}
defer lip.Body.Close()
loc, err := ioutil.ReadAll(lip.Body)
if err != nil {
log.Fatal(err)
os.Exit(0)
}
lstring := string(loc)
fmt.Println(lstring)
os.Exit(0)
}
output
{
"ip": "216.58.208.78",
"city": "Mountain View",
"region": "California",
"country": "US",
"loc": "37.4192,-122.0570",
"postal": "94043"
}
I want to get output just like this
ip: 216.58.208.78,
city: Mountain View,
region: California,
country: US,
loc: 37.4192,-122.0570,
postal: 94043
not in json format
You need to do two things:
You need to unmarshal the JSON you have received from the server. The json package will do this for you.
You need to output the data in a format you desire. You can just use fmt.Printf
or similar for that.