在GAE下使用http.Server时运行时出现恐慌错误。

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

Runtime panic when using http.Server under GAE

问题

我的代码在这里:
http://play.golang.org/p/RehA28iJtA

为什么当我在浏览器中输入"http://example.com:8080/download"时会显示以下错误?

错误信息:

运行时进程返回了错误的HTTP响应:''

2014/05/14 13:07:50 http: panic serving 127.0.0.1:59525: 运行时错误:无效的内存地址或空指针解引用
goroutine 3 [running]:
net/http.func·009()
/tmp/appengine/go_appengine/goroot/src/pkg/net/http/server.go:1093 +0xae
runtime.panic(0x646520, 0x94ba48)
/tmp/appengine/go_appengine/goroot/src/pkg/runtime/panic.c:248 +0x106
xflight.Download(0x7fcade6926b8, 0xc21000f6e0, 0xc21005ea90)
xflight/xflight.go:21 +0x132
net/http.HandlerFunc.ServeHTTP(0x71b6e8, 0x7fcade6926b8, 0xc21000f6e0, 0xc21005ea90)
/tmp/appengine/go_appengine/goroot/src/pkg/net/http/server.go:1220 +0x40
net/http.(*ServeMux).ServeHTTP(0xc21001e630, 0x7fcade6926b8, 0xc21000f6e0, 0xc21005ea90)
/tmp/appengine/go_appengine/goroot/src/pkg/net/http/server.go:1496 +0x163
appengine_internal.handleFilteredHTTP(0x7fcade6926b8, 0xc21000f6e0, 0xc21005ea90)
/tmp/appengine/go_appengine/goroot/src/pkg/appengine_internal/api_dev.go:98 +0x2e2
net/http.HandlerFunc.ServeHTTP(0x6fe040, 0x7fcade6926b8, 0xc21000f6e0, 0xc21005ea90)
/tmp/appengine/go_appengine/goroot/src/pkg/net/http/server.go:1220 +0x40
net/http.serverHandler.ServeHTTP(0xc21000b640, 0x7fcade6926b8, 0xc21000f6e0, 0xc21005ea90)
/tmp/appengine/go_appengine/goroot/src/pkg/net/http/server.go:1597 +0x16e
net/http.(*conn).serve(0xc210058380)
/tmp/appengine/go_appengine/goroot/src/pkg/net/http/server.go:1167 +0x7b7
created by net/http.(*Server).Serve
/tmp/appengine/go_appengine/goroot/src/pkg/net/http/server.go:1644 +0x28b

英文:

My code is here:
http://play.golang.org/p/RehA28iJtA

Why when I enter "http://example.com:8080/download" to the browser it shows a following error?

Error:

the runtime process gave a bad HTTP response: ''

2014/05/14 13:07:50 http: panic serving 127.0.0.1:59525: runtime error: invalid memory address or nil pointer dereference
goroutine 3 [running]:
net/http.func·009()
	/tmp/appengine/go_appengine/goroot/src/pkg/net/http/server.go:1093 +0xae
runtime.panic(0x646520, 0x94ba48)
	/tmp/appengine/go_appengine/goroot/src/pkg/runtime/panic.c:248 +0x106
xflight.Download(0x7fcade6926b8, 0xc21000f6e0, 0xc21005ea90)
	xflight/xflight.go:21 +0x132
net/http.HandlerFunc.ServeHTTP(0x71b6e8, 0x7fcade6926b8, 0xc21000f6e0, 0xc21005ea90)
	/tmp/appengine/go_appengine/goroot/src/pkg/net/http/server.go:1220 +0x40
net/http.(*ServeMux).ServeHTTP(0xc21001e630, 0x7fcade6926b8, 0xc21000f6e0, 0xc21005ea90)
	/tmp/appengine/go_appengine/goroot/src/pkg/net/http/server.go:1496 +0x163
appengine_internal.handleFilteredHTTP(0x7fcade6926b8, 0xc21000f6e0, 0xc21005ea90)
	/tmp/appengine/go_appengine/goroot/src/pkg/appengine_internal/api_dev.go:98 +0x2e2
net/http.HandlerFunc.ServeHTTP(0x6fe040, 0x7fcade6926b8, 0xc21000f6e0, 0xc21005ea90)
	/tmp/appengine/go_appengine/goroot/src/pkg/net/http/server.go:1220 +0x40
net/http.serverHandler.ServeHTTP(0xc21000b640, 0x7fcade6926b8, 0xc21000f6e0, 0xc21005ea90)
	/tmp/appengine/go_appengine/goroot/src/pkg/net/http/server.go:1597 +0x16e
net/http.(*conn).serve(0xc210058380)
	/tmp/appengine/go_appengine/goroot/src/pkg/net/http/server.go:1167 +0x7b7
created by net/http.(*Server).Serve
	/tmp/appengine/go_appengine/goroot/src/pkg/net/http/server.go:1644 +0x28b

答案1

得分: 4

你不能在App Engine中使用默认的HTTP客户端。你需要在这里使用URL Fetch服务 https://developers.google.com/appengine/docs/go/urlfetch/
示例:http://play.golang.org/p/Gf-WzLmP09

英文:

You cannot use default http client in App Engine. You need to use URL Fetch service here https://developers.google.com/appengine/docs/go/urlfetch/
Example: http://play.golang.org/p/Gf-WzLmP09

答案2

得分: 0

在你的示例中,func main() 缺失。

参见playground示例

顺便说一下,在 func Download() 中,你使用的是 string 而不是 variable,如果你访问 http://127.0.0.1:8080/download,你总是会得到 "resp"。

英文:

In your example func main() is missing.

See playground example

Btw, in func Download() - you use string not variable, and if you go to http://127.0.0.1:8080/download - you always will get "resp".

huangapple
  • 本文由 发表于 2014年5月14日 21:12:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/23655766.html
匿名

发表评论

匿名网友

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

确定