Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/vet: Detect global variables := assignments #24867

gertcuykens opened this issue Apr 15, 2018 · 3 comments


None yet
4 participants
Copy link

commented Apr 15, 2018

var mux *http.ServeMux

func init() {
	mux := http.NewServeMux()
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "TLS: %+v", r.TLS)

func TestACME(t *testing.T) {
	m := &autocert.Manager{
		Cache:      autocert.DirCache("tls"),
		Prompt:     autocert.AcceptTOS,
		HostPolicy: autocert.HostWhitelist(""),
	go http.ListenAndServe(":http", m.HTTPHandler(nil))
	s := &http.Server{
		Addr:      ":https",
		TLSConfig: &tls.Config{GetCertificate: m.GetCertificate},
		Handler:   mux,
	t.Fatal(s.ListenAndServeTLS("", ""))

go test -v
Then visit the website

2018/04/15 04:49:57 http2: panic serving ...:62672: runtime error: invalid memory address or nil pointer dereference
goroutine 16 [running]:
net/http.(*http2serverConn).runHandler.func1(0xc42000e0a8, 0xc42003cfaf, 0xc42019e380)
	/usr/lib/go/src/net/http/h2_bundle.go:5753 +0x192
panic(0x7f7ffea9a9c0, 0x7f7ffec35a90)
	/usr/lib/go/src/runtime/panic.go:502 +0x22d
net/http.(*ServeMux).shouldRedirect(0x0, 0xc420015fb0, 0xd, 0x7f7ffe7c3bcc, 0x1, 0x7f7ffe6f868d)
	/usr/lib/go/src/net/http/server.go:2239 +0xf3
net/http.(*ServeMux).redirectToPathSlash(0x0, 0xc420015fb0, 0xd, 0x7f7ffe7c3bcc, 0x1, 0xc420102700, 0xff3, 0xc42007f720)
	/usr/lib/go/src/net/http/server.go:2224 +0x59
net/http.(*ServeMux).Handler(0x0, 0xc4201b6200, 0xc42019adc0, 0xc420030d01, 0xc420030da8, 0x7f7ffe6fd0c8)
	/usr/lib/go/src/net/http/server.go:2293 +0x11a
net/http.(*ServeMux).ServeHTTP(0x0, 0x7f7ffeb00dc0, 0xc42000e0a8, 0xc4201b6200)
	/usr/lib/go/src/net/http/server.go:2336 +0x100
net/http.serverHandler.ServeHTTP(0xc420071040, 0x7f7ffeb00dc0, 0xc42000e0a8, 0xc4201b6200)
	/usr/lib/go/src/net/http/server.go:2694 +0xbe
net/http.initNPNRequest.ServeHTTP(0xc42007f500, 0xc420071040, 0x7f7ffeb00dc0, 0xc42000e0a8, 0xc4201b6200)
	/usr/lib/go/src/net/http/server.go:3260 +0x9c
net/http.(Handler).ServeHTTP-fm(0x7f7ffeb00dc0, 0xc42000e0a8, 0xc4201b6200)
	/usr/lib/go/src/net/http/h2_bundle.go:5475 +0x4f
net/http.(*http2serverConn).runHandler(0xc42019e380, 0xc42000e0a8, 0xc4201b6200, 0xc42019ae80)
	/usr/lib/go/src/net/http/h2_bundle.go:5760 +0x8b
created by net/http.(*http2serverConn).processHeaders
	/usr/lib/go/src/net/http/h2_bundle.go:5494 +0x46d
2018/04/15 04:49:57 http2: panic serving ...:62672: runtime error: invalid memory address or nil pointer dereference

Noobs like me are ??? for many hours. go vet should warn are you really sure you want a local var instead of the global one mux =


This comment has been minimized.

Copy link

commented Apr 15, 2018

It does if you go vet -shadow=true


This comment has been minimized.

Copy link

commented Apr 15, 2018

Yes, and this setting is experimental (not default) meaning that sometimes it is desired to shadow the global var.


This comment has been minimized.

Copy link

commented Apr 15, 2018

Doh! missed that sorry

@golang golang locked and limited conversation to collaborators Apr 15, 2019

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.