當(dāng)我在 LiteIDE 中運(yùn)行此代碼時(shí),通過 build 和 run 命令,它可以工作。但是當(dāng)我運(yùn)行它時(shí)go run scraper.go或者go build scraper.go
./scraper它在 r.Body.Close() 行中失敗并出現(xiàn)錯誤panic: runtime error: invalid memory address or nil pointer dereference這是違規(guī)代碼:r, err := http.Get(job.Url)
defer r.Body.Close() //same error with or without defer腳本在這里:https : //gist.github.com/meddulla/5934457但它基本上接受通過 post 請求抓取的 url,例如curl -X POST -d "[{\"url\": \"http://localhost:8888/IBTX/proj/dev/article.html\"}]" http://localhost:8080/jobs/add我不明白為什么它可以在 liteIde 中工作,但當(dāng)我直接在終端中運(yùn)行它時(shí)卻不能(程序啟動正常,所以它不是 GOPATH 設(shè)置或其他東西,它只在響應(yīng)發(fā)布請求時(shí)失?。┤魏蜗敕槭裁??
1 回答

動漫人物
TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超10個(gè)贊
您首先需要檢查 err 是否為零。
r, err := http.Get(job.Url)
if err != nil {
log.Fatal(err)
}
defer r.Body.Close() //same error with or without defer
- 1 回答
- 0 關(guān)注
- 258 瀏覽
添加回答
舉報(bào)
0/150
提交
取消