有人可以解释为什么以下gocode与goapp服务失败

时间:2014-06-09 00:02:20

标签: google-app-engine go

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()可解决问题。我的问题是为什么会发生这种情况?

1 个答案:

答案 0 :(得分:4)

启动运行时进程时,Go Development Server(在App Engine Go SDK中)正在读取helloworld的init中的单行响应。

修改_start_process_flavor中的http_runtime.py标记;因此,HTTP运行时尝试读取该行以获取要侦听的端口的方向。

  

读取启动过程文件中预期的单行响应。 [...] START_PROCESS_FILE flavor使用运行时实例的文件来报告它正在侦听的端口。

在这种情况下,hello不是一个有效的侦听端口。


请尝试使用Go的log包:

log.Print("hello")