Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 程序在主块中捕捉到错误后陷入恐慌。死机:运行时错误:无效内存地址或零指针取消引用_Go - Fatal编程技术网

Go 程序在主块中捕捉到错误后陷入恐慌。死机:运行时错误:无效内存地址或零指针取消引用

Go 程序在主块中捕捉到错误后陷入恐慌。死机:运行时错误:无效内存地址或零指针取消引用,go,Go,我是刚来戈朗的。在定义了位置后,我试图捕获主块中的错误,程序陷入恐慌。我在某个地方读过,添加defer.close()可能会有所帮助,但编译器再次指出,您的结构中不存在这样的定义,请帮助我解决它 type IPInfo struct { IP string Hostname string City string Country string Loc string Org string Postal string } func mai

我是刚来戈朗的。在定义了位置后,我试图捕获主块中的错误,程序陷入恐慌。我在某个地方读过,添加defer.close()可能会有所帮助,但编译器再次指出,您的结构中不存在这样的定义,请帮助我解决它

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"
}
您的
ForeignIP()
返回
*IPInfo
这是一个指针类型的结构,但您从这行代码
响应中得到错误,错误:=http.Get(“http://ipinfo.io“+ip+”/json”)
由于以下错误而失败:

拨号tcp:lookup ipinfo.io172.16.11.115:没有这样的主机

然后你像那样返回那边的
nil

   if err != nil {
            return nil , err
   }
您可以通过以下方式访问nil的值:

location, err := ForeignIP(ip)
fmt.Println("ip address of this machine is ", location.IP)
fmt.Println(" city of this machine is ", location.City)
所以
nil
值没有任何
IP
City
变量,这就是它恐慌的原因。因此,您需要返回指针类型的struct,然后您可以从
location
response访问变量
IP
City
,这里我正在修改代码

func ForeignIP(ip string) (*IPInfo, error) {
    var ipinfo IPInfo
    if ip != "" {
        ip += "/" + ip
    }
    response, err := http.Get("http://ipinfo.io" + ip + "/json")
    if err != nil {
        return &ipinfo , err
    }

    defer response.Body.Close()

    contents, err := ioutil.ReadAll(response.Body)
    if err != nil {
        return &ipinfo, err
    }

    if err := json.Unmarshal(contents, &ipinfo); err != nil {
        return &ipinfo, err
    }
    return &ipinfo, nil
}