英文:
Could someone explain why the following gocode fails with goapp serve
问题
以下是您提供的代码的中文翻译:
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
输出中出现以下错误:
(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()`可以解决此问题。我的问题是为什么会发生这种情况?
<details>
<summary>英文:</summary>
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?
</details>
# 答案1
**得分**: 4
在启动运行时进程时,[Go开发服务器](https://developers.google.com/appengine/docs/go/tools/devserver)(在App Engine Go SDK中)会读取在你的helloworld的`init`中找到的单行响应。
这会修改`http_runtime.py`中的`_start_process_flavor`标志;因此,HTTP运行时会尝试读取该行以确定监听哪个端口。
> 读取在启动进程文件中预期的单行响应。[...] START_PROCESS_FILE flavor 使用一个文件来报告运行时实例正在监听的端口。
在这种情况下,`hello`不是一个有效的监听端口。
----------
尝试使用Go的[`log`](http://golang.org/pkg/log/)包代替:
log.Print("hello")
<details>
<summary>英文:</summary>
When starting the runtime process, the [Go Development Server](https://developers.google.com/appengine/docs/go/tools/devserver) (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 in `http_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`](http://golang.org/pkg/log/) package instead:
log.Print("hello")
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论