简体   繁体   中英

Runtime panic when using http.Server under GAE

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

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

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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM