有人可以解释一下为什么下面的gocode在使用goapp serve时失败了吗?

huangapple go评论83阅读模式
英文:

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 (
      &quot;fmt&quot;
      &quot;net/http&quot;
    
      &quot;appengine&quot;
      &quot;appengine/user&quot;
    )
    
    func init() {
      fmt.Print(&quot;hello&quot;)
      http.HandleFunc(&quot;/&quot;, 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(&quot;Location&quot;, url)
        w.WriteHeader(http.StatusFound)
        return
      }
      fmt.Fprintf(w, &quot;Hello, %v!&quot;, 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 &quot;default&quot; 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 [&#39;hello46591\n&#39;]

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运行时会尝试读取该行以确定监听哪个端口。

&gt; 读取在启动进程文件中预期的单行响应。[...] 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&#39;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.

&gt; 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&#39;t a valid port to listen on.


----------

Try using Go&#39;s [`log`](http://golang.org/pkg/log/) package instead:

    log.Print(&quot;hello&quot;)

</details>



huangapple
  • 本文由 发表于 2014年6月9日 08:02:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/24112121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定