2014-05-14 74 views
1

下使用時http.Server我的代碼是在這裏: http://play.golang.org/p/RehA28iJtA運行時的恐慌GAE

爲什麼當我進入「http://example.com:8080/download」它顯示了以下錯誤的瀏覽器?

錯誤:

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 

回答

3

在App Engine中不能使用默認的HTTP客戶端。您需要在這裏使用網址提取服務https://developers.google.com/appengine/docs/go/urlfetch/
示例:http://play.golang.org/p/Gf-WzLmP09

+0

謝謝你幫我一點。但是如何實際顯示響應(在這種情況下是html)?現在它只是給了我一些奇怪的文字,當我這樣做:http://play.golang.org/p/j43ILUifBo – SmiglowiecX

+0

http://play.golang.org/p/7Xo8dUvfi-你直接打印身體,這將只是作爲內存地址出來。您應該將數據從正文複製到響應中。 – Logiraptor

+0

@SmiglowiecX那另一個問題! 'resp.Body'是一個'io.Reader'界面(認爲它像一個流),@ Logiraptor有你的答案。 – nvcnvn

0

在您的示例中,缺少func main()。

playground example

順便說一下,在func Download() - 您使用stringvariable,如果你去http://127.0.0.1:8080/download - 你總是會得到 「響應」。