【问题标题】:Could someone explain why the following gocode fails with goapp serve有人可以解释为什么以下 gocode 会因 goapp serve 而失败
【发布时间】:2014-06-09 00:02:20
【问题描述】:
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() 解决了这个问题。我的问题是为什么会这样?

【问题讨论】:

  • 我对 Go 一无所知。但是看看 fmt.Fprintf 它期望 w 是 io.Writer ,我认为 http.ResponseWriter 不符合那个接口。

标签: google-app-engine go


【解决方案1】:

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

这会修改http_runtime.py 中的_start_process_flavor 标志;因此,HTTP 运行时会尝试读取该行以了解侦听端口的方向。

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

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


尝试改用 Go 的 log 包:

log.Print("hello")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    • 2010-09-09
    • 2022-08-20
    相关资源
    最近更新 更多