我有興趣在 Go 1.13 中運行https://github.com/cavaliercoder/grab包的單元測試。雖然我的GO111MODULE環境通常是,但我已經使用以下命令on將包下載到我的:GOPATHenv GO111MODULE=off go get -u -d github.com/cavaliercoder/grab在結果grab目錄中,我運行了go mod init以下命令go.mod:module github.com/cavaliercoder/grab
go 1.13現在,如果我嘗試運行go test,我會收到以下錯誤:> go testpanic: runtime error: invalid memory address or nil pointer dereference [recovered] panic: runtime error: invalid memory address or nil pointer dereference[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x12cc70d]goroutine 1556 [running]:testing.tRunner.func1(0xc00010ea00) /usr/local/Cellar/go/1.13/libexec/src/testing/testing.go:874 +0x3a3panic(0x132ba20, 0x1629ed0) /usr/local/Cellar/go/1.13/libexec/src/runtime/panic.go:679 +0x1b2github.com/cavaliercoder/grab.guessFilename(0xc00052aed0, 0xc0000aa008, 0x13951b5, 0x3, 0x139c440) /Users/kurt/go/src/github.com/cavaliercoder/grab/util.go:51 +0x2dgithub.com/cavaliercoder/grab.TestURLFilenames.func2(0xc00010ea00) /Users/kurt/go/src/github.com/cavaliercoder/grab/util_test.go:54 +0x123testing.tRunner(0xc00010ea00, 0x13afdf8) /usr/local/Cellar/go/1.13/libexec/src/testing/testing.go:909 +0xc9created by testing.(*T).Run /usr/local/Cellar/go/1.13/libexec/src/testing/testing.go:960 +0x350exit status 2FAIL github.com/cavaliercoder/grab 2.531s我查看了有問題的測試文件,util_test.go但找不到任何問題:package grabimport ( "fmt" "net/http" "net/url" "testing")func TestURLFilenames(t *testing.T) { t.Run("Valid", func(t *testing.T) { expect := "filename" testCases := []string{ "http://test.com/filename", "http://test.com/path/filename", "http://test.com/deep/path/filename", "http://test.com/filename?with=args", "http://test.com/filename#with-fragment", "http://test.com/filename?with=args&and#with-fragment", }錯誤消息似乎說這req是一個空指針,但由于它是使用定義的,所以http.NewRequest()我不明白這是怎么回事?
1 回答

慕村225694
TA貢獻1880條經驗 獲得超4個贊
添加此錯誤打?。?/p>
req, err1 := http.NewRequest("GET", tc, nil) if err1 != nil { log.Println(err1.Error()) }
它會打印:
解析http://test.com/filename : net/url: URL 中的控制字符無效
這意味著該網址"http://test.com/filename\x00"
不被允許。
注釋掉這一行然后它就可以工作了:
[p1gd0g@p1gd0g-pc grab]$ go test PASS ok github.com/cavaliercoder/grab 2.586s
- 1 回答
- 0 關注
- 173 瀏覽
添加回答
舉報
0/150
提交
取消