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,我是一个新手,正在尝试制作一个简单的webcrawler。我一直收到“恐慌:运行时错误:无效内存地址或零指针解引用”,不知道如何解决这个问题。我有一个“advancedFetcher”函数和一个“basicFetcher”函数,在这两个函数下我都会得到相同的错误。建议检查每个错误(我想我有),但我仍然得到一个错误。谢谢 package main import ( "crypto/tls" "fmt" "io/ioutil" "net" "net/http

我是一个新手,正在尝试制作一个简单的webcrawler。我一直收到“恐慌:运行时错误:无效内存地址或零指针解引用”,不知道如何解决这个问题。我有一个“advancedFetcher”函数和一个“basicFetcher”函数,在这两个函数下我都会得到相同的错误。建议检查每个错误(我想我有),但我仍然得到一个错误。谢谢

package main

import (
    "crypto/tls"
    "fmt"
    "io/ioutil"
    "net"
    "net/http"
    "time"
)

var tr = &http.Transport{
    TLSClientConfig:     &tls.Config{InsecureSkipVerify: true},
    MaxIdleConnsPerHost: 25,
    DisableKeepAlives:   true,
    Proxy:               http.ProxyFromEnvironment,
    Dial: (&net.Dialer{
        Timeout:   10 * time.Second,
        KeepAlive: 10 * time.Second,
    }).Dial,
    TLSHandshakeTimeout: 5 * time.Second,
}
var client = &http.Client{Transport: tr}

func makeGetRequest(uri string) *http.Response {
    req, err := http.NewRequest("GET", uri, nil)
    if err != nil {
        fmt.Println(err)
    }
    req.Header.Add("User-Agent", "Hi it's me again")
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println(err)
    }
    return resp
}

func advancedFetcher(uri string, c chan int) {
    fmt.Println("Getting: ", uri)
    resp := makeGetRequest(uri)
    defer resp.Body.Close()
    c <- resp.StatusCode
}

func basicFetcher(uri string, c chan int) {
    fmt.Println("Getting: ", uri)
    resp, err := http.Get(uri)
    if err != nil {
        fmt.Println(err)
    }
    defer resp.Body.Close()
    _, err = ioutil.ReadAll(resp.Body)
    c <- resp.StatusCode
}

func main() {
    c := make(chan int)
    rows := []string{"https://www.google.com", "http://www.fdicconnect.gov"}
    for _, row := range rows {
        //go basicFetcher(row, c)
        go advancedFetcher(row, c)
    }
    for _ = range rows {
        select {
        case result := <-c:
            fmt.Println(result)
        }
    }
    fmt.Println("All Done")
}

您的程序在恐慌之前打印错误吗?如果内存可用,如果
http.Get
返回错误,则
resp.Body==nil
,这将导致错误,因为您无法在
nil
上调用
resp.Body.Close()


同样,您应该编写函数,使其返回(结果、错误)对,以便在遇到问题之前可以优雅地终止它。

您的程序在恐慌之前打印错误吗?如果内存可用,如果
http.Get
返回错误,
resp.Body
nil
,这将导致您的错误。@JimmySawczuk:您的意思是类似于Edit#1?如果是这样的话,我还是会犯同样的错误。是哪条线引起恐慌?是的,包括撞车。通常会使这样的东西更容易调试。@two我添加了crashdump。第41行表示“advancedFetcher”函数中的“defer resp.Body.Close()”。
Getting:  https://www.google.com
Getting:  http://www.fdicconnect.gov
200
Get http://www.fdicconnect.gov: dial tcp 167.176.6.86:80: i/o timeout
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x400ff2]

goroutine 21 [running]:
runtime.panic(0x6594c0, 0x7e9b53)
    /usr/local/go/src/pkg/runtime/panic.c:279 +0xf5
main.advancedFetcher(0x6c3b70, 0x1a, 0xc208004180)
    /home/me/go/src/testfolder/tester/main.go:41 +0x1c2
created by main.main
    /home/me/go/src/testfolder/tester/main.go:61 +0x123

goroutine 16 [chan receive]:
main.main()
    /home/me/go/src/testfolder/tester/main.go:65 +0x198

goroutine 19 [finalizer wait]:
runtime.park(0x413bd0, 0x7ee660, 0x7ec8c9)
    /usr/local/go/src/pkg/runtime/proc.c:1369 +0x89
runtime.parkunlock(0x7ee660, 0x7ec8c9)
    /usr/local/go/src/pkg/runtime/proc.c:1385 +0x3b
runfinq()
    /usr/local/go/src/pkg/runtime/mgc0.c:2644 +0xcf
runtime.goexit()
    /usr/local/go/src/pkg/runtime/proc.c:1445

goroutine 17 [syscall]:
runtime.goexit()
    /usr/local/go/src/pkg/runtime/proc.c:1445