Golang: runtime error: invalid memory address or dereferencing of a zero point pointer

I am new to golang and currently follow this tutorial and source code here - http://golang.org/doc/articles/wiki/part2.go

After creating this file, I get

calvin$ ./mywebwiki2 2012/07/23 17:12:59 http: panic serving [::1]:58820: runtime error: invalid memory address or nil pointer dereference /usr/local/go/src/pkg/net/http/server.go:576 (0x3f202) _func_003: buf.Write(debug.Stack()) /private/tmp/bindist454984655/go/src/pkg/runtime/proc.c:1443 (0x10c79) /private/tmp/bindist454984655/go/src/pkg/runtime/runtime.c:128 (0x11745) /private/tmp/bindist454984655/go/src/pkg/runtime/thread_darwin.c:418 (0x148b5) /Users/calvin/work/gowiki/mywebwiki2.go:33 (0x2248) viewHandler: fmt.Fprintf(w, "<h1>%s</h1><div>%s</div>", p.Title, p.Body) /usr/local/go/src/pkg/net/http/server.go:690 (0x331ae) HandlerFunc.ServeHTTP: f(w, r) /usr/local/go/src/pkg/net/http/server.go:926 (0x34030) (*ServeMux).ServeHTTP: mux.handler(r).ServeHTTP(w, r) /usr/local/go/src/pkg/net/http/server.go:656 (0x32fc1) (*conn).serve: handler.ServeHTTP(w, w.req) /private/tmp/bindist454984655/go/src/pkg/runtime/proc.c:271 (0xed7f) 2012/07/23 17:12:59 http: panic serving [::1]:58821: runtime error: invalid memory address or nil pointer dereference 

Any idea what I did wrong to cause this apparent memory corruption?

+7
source share
2 answers

There, err is ignored on line 36. The error probably says open .txt: no such file or directory if you tested in the browser using the URL http://localhost:8080/view/ or open foo.txt: no such file or directory if you tested in a browser using the URL http://localhost:8080/view/foo . In the latter case, the file "foo.txt" must be in your working directory for this sample code to work. After that, the code seems to work for me locally.

Someone should probably fill out a question about the ignored meaning of the error.

+14
source

In the tutorial, you previously created the TestPage file. Which page should you go to when you first create the server. In the tutorial, you can go to view/test instead of view/TestPage as you should, which creates confusion.

0
source

All Articles