Wednesday 10 April 2019

GoLang - weirdness with "panic: assignment to entry in nil map"

I kept seeing this: -

--- FAIL: TestClient (0.00s)
panic: assignment to entry in nil map [recovered]
panic: assignment to entry in nil map

goroutine 5 [running]:
testing.tRunner.func1(0xc42011e0f0)
/Users/hayd/Downloads/go/src/testing/testing.go:742 +0x29d
panic(0x128d080, 0x1314b50)
/Users/hayd/Downloads/go/src/runtime/panic.go:502 +0x229
net/textproto.MIMEHeader.Add(0x0, 0x12e2d99, 0xd, 0x12e1ac2, 0x8)
/Users/hayd/Downloads/go/src/net/textproto/header.go:15 +0xec
net/http.Header.Add(0x0, 0x12e2f05, 0xd, 0x12e1ac2, 0x8)
/Users/hayd/Downloads/go/src/net/http/header.go:24 +0x53
github.com/david-hay/GoStuff/cmd/sparkles.glob..func3(0x12e1aba, 0x8, 0x12e1ea3, 0x9, 0xc42006c800, 0x0, 0x0, 0x0, 0x0)
/Users/hayd/go/src/github.com/david-hay/GoStuff/cmd/sparkles/sparkles.go:38 +0xe1
github.com/david-hay/GoStuff/cmd/sparkles.TestClient(0xc42011e0f0)
/Users/hayd/go/src/github.com/david-hay/GoStuff/cmd/sparkles/sparkles_test.go:42 +0xc6
testing.tRunner(0xc42011e0f0, 0x12f4678)
/Users/hayd/Downloads/go/src/testing/testing.go:777 +0xd0
created by testing.(*T).Run
/Users/hayd/Downloads/go/src/testing/testing.go:824 +0x2e0
FAIL github.com/david-hay/GoStuff/cmd/sparkles 0.033s

when attempting to test my very basic HTTP code, using Visual Studio Code or just through the GoLang CLI: -

go test ./...

or: -

go test -v ./...

Looking at the stack trace, I saw reference to line 38 of my code: -

req.Header.Add("cache-control", "no-cache")

I commented that out, and the test just ran :-)

Not sure why that line causes that particular exception, given that I'm not actually using a Map, but ....

C'est la vie

No comments:

Visual Studio Code - Wow 🙀

Why did I not know that I can merely hit [cmd] [p]  to bring up a search box allowing me to search my project e.g. a repo cloned from GitHub...