有人可以解释为什么以下gocode与goapp服务失败 [英] Could someone explain why the following gocode fails with goapp serve
问题描述
package helloworld
导入(
fmt
net / http
appengine
appengine / user
)
func init(){
fmt.Print(hello)
http.HandleFunc(/ ,处理程序)
}
func处理程序(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)
}
code>
在 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]跳过SDK更新检查。
INFO 2014-06-08 23:57:47,877 api_server.py:171]启动API服务器:http:// localhost:48026
INFO 2014-06-08 23:57:47,923调度程序。 py:182]启动模块default运行在:http://0.0.0.0:8080
INFO 2014-06-08 23:57:47,925 admin_server.py:117]启动管理服务器:http:/ / localhost:8000
ERROR 2014-06-08 23:57:48,759 http_runtime.py:262]错误的运行时进程端口['hello46591 \ n']
删除 fmt.Print()
可以解决问题。我的问题是为什么会发生这种情况?
当启动运行时进程时, Go Development Server (位于App Engine Go SDK中)正在读取您的helloworld <$ c $中的单行响应c> init 。
这会修改 _start_process_flavor
标志> http_runtime.py ;因此,HTTP运行时会尝试读取线路以侦听哪个端口。
读取开始时预计的单行响应处理文件。 [...] START_PROCESS_FILE flavor为运行时实例使用文件来报告它正在监听的端口。例如,
hello
不是有效的端口。
<请尝试使用Go的log
软件包:log.Print(hello)
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) }
Throw the following error in
goapp serve
output(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']
Removing the
fmt.Print()
fixes the issue. My question is why does that happen?解决方案When starting the runtime process, the Go Development Server (in the App Engine Go SDK) is reading the single line response found in your helloworld's
init
.This modifies the
_start_process_flavor
flag inhttp_runtime.py
; consequently, the HTTP runtime attempts to read the line for direction on which port to listen to.Read the single line response expected in the start process file. [...] The START_PROCESS_FILE flavor uses a file for the runtime instance to report back the port it is listening on.
In this case,
hello
isn't a valid port to listen on.
Try using Go's
log
package instead:log.Print("hello")
这篇关于有人可以解释为什么以下gocode与goapp服务失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!