I am new to golang. My program panics after I try to catch an error in the main block after getting location defined. I have read somewhere, adding defer.close() might help, but again the compiler says no such definition exists in your struct Help me resolve it.
type IPInfo struct {
IP string
Hostname string
City string
Country string
Loc string
Org string
Postal string
}
func main() {
ip := getLocalIpv4()
location, err := ForeignIP(ip)
if err != nil {
fmt.Println("error bro")
}
fmt.Println("ip address of this machine is ", location.IP)
fmt.Println(" city of this machine is ", location.City)
}
// MyIP provides information about the public IP address of the client.
func MyIP() (*IPInfo, error) {
return ForeignIP("")
}
// ForeignIP provides information about the given IP address,
// which should be in dotted-quad form.
func ForeignIP(ip string) (*IPInfo, error) {
if ip != "" {
ip += "/" + ip
}
response, err := http.Get("http://ipinfo.io" + ip + "/json")
if err != nil {
return nil, err
}
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
var ipinfo IPInfo
if err := json.Unmarshal(contents, &ipinfo); err != nil {
return nil, err
}
return &ipinfo, nil
}
// retrieves ip address of local machine
func getLocalIpv4() string {
host, _ := os.Hostname()
addrs, _ := net.LookupIP(host)
for _, addr := range addrs {
if ipv4 := addr.To4(); ipv4 != nil {
return fmt.Sprintf("%s", ipv4)
}
}
return "localhost"
}