golang: 运行时错误:无效的内存地址或空指针解引用

huangapple go评论87阅读模式
英文:

golang: runtime error: invalid memory address or nil pointer dereference

问题

我是新手使用golang,目前正在按照这个教程和源代码进行操作 - http://golang.org/doc/articles/wiki/part2.go

构建这个文件后,我得到了以下错误信息:

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

有任何想法是我做错了什么导致这个明显的内存损坏吗?

英文:

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

After building this file, I am getting

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 be causing this apparent memory corruption?

答案1

得分: 16

在第36行有一个被忽略的错误。如果你在浏览器中使用URL http://localhost:8080/view/ 进行测试,错误可能会显示为 open .txt: no such file or directory,如果你在浏览器中使用URL http://localhost:8080/view/foo 进行测试,错误可能会显示为 open foo.txt: no such file or directory。在后一种情况下,你的工作目录中必须有一个名为 "foo.txt" 的文件才能使这个示例代码正常工作。之后,这段代码在我的本地似乎工作正常。

应该有人提出一个关于被忽略的错误值的问题。

英文:

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

Someone should probably fill an issue about the ignored error value.

答案2

得分: 1

在教程中,你之前创建了名为TestPage的文件。这是你在首次构建服务器时应该导航到的页面。在教程中,他们让你导航到view/test而不是view/TestPage,这就造成了混淆。

英文:

In the tutorial, you created the file TestPage previously. Which is the page you should be navigating to when you first build the server. In the tutorial, they have you navigate to view/test instead of view/TestPage as you should which is what creates the confusion.

huangapple
  • 本文由 发表于 2012年7月23日 17:16:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/11609583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定