package helloworld import ( "fmt" "net/http" "appengine" "appengine/user" ) func init() { fmt.Print("hello") http.HandleFunc("/", handler) } func handler(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) u := user.Current(c) if u == nil { url, err := user.LoginURL(c, r.URL.String()) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Location", url) w.WriteHeader(http.StatusFound) return } fmt.Fprintf(w, "Hello, %v!", u) }
在goapp serve输出中引发以下错误
goapp serve
(saucy)adam@localhost:~/projects/ringed-land-605/default$ goapp serve -host 0.0.0.0 . INFO 2014-06-08 23:57:47,862 devappserver2.py:716] Skipping SDK update check. INFO 2014-06-08 23:57:47,877 api_server.py:171] Starting API server at: http://localhost:48026 INFO 2014-06-08 23:57:47,923 dispatcher.py:182] Starting module "default" running at: http://0.0.0.0:8080 INFO 2014-06-08 23:57:47,925 admin_server.py:117] Starting admin server at: http://localhost:8000 ERROR 2014-06-08 23:57:48,759 http_runtime.py:262] bad runtime process port ['hello46591\n']
删除fmt.Print()修复问题。我的问题是为什么会这样?
fmt.Print()
开始运行时过程时,Go Development Server(在App Engine Go SDK中)正在读取helloworld的中找到的单行响应init。
init
这会修改中的_start_process_flavor标志http_runtime.py;因此,HTTP运行时会尝试读取行以指示要侦听哪个端口。
_start_process_flavor
http_runtime.py
阅读启动过程文件中预期的单行响应。[…] START_PROCESS_FILE版本使用运行时实例的文件来报告其正在侦听的端口。
在这种情况下,hello不是要监听的有效端口。
hello
尝试改用Go的log软件包:
log
log.Print("hello")