Could someone explain why the following gocode fai

2019-07-05 09:08发布

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?

1条回答
小情绪 Triste *
2楼-- · 2019-07-05 09:52

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 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 package instead:

log.Print("hello")
查看更多
登录 后发表回答