小编典典

有人可以解释为什么以下gocode不能通过goapp服务失败

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()修复问题。我的问题是为什么会这样?


阅读 335

收藏
2020-07-02

共1个答案

小编典典

开始运行时过程时,Go Development
Server
(在App
Engine Go SDK中)正在读取helloworld的中找到的单行响应init

这会修改中的_start_process_flavor标志http_runtime.py;因此,HTTP运行时会尝试读取行以指示要侦听哪个端口。

阅读启动过程文件中预期的单行响应。[…] START_PROCESS_FILE版本使用运行时实例的文件来报告其正在侦听的端口。

在这种情况下,hello不是要监听的有效端口。


尝试改用Go的log软件包:

log.Print("hello")
2020-07-02