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

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

Could someone explain why the following gocode fails with goapp serve

问题

以下是您提供的代码的中文翻译:

  1. package helloworld
  2. import (
  3. "fmt"
  4. "net/http"
  5. "appengine"
  6. "appengine/user"
  7. )
  8. func init() {
  9. fmt.Print("hello")
  10. http.HandleFunc("/", handler)
  11. }
  12. func handler(w http.ResponseWriter, r *http.Request) {
  13. c := appengine.NewContext(r)
  14. u := user.Current(c)
  15. if u == nil {
  16. url, err := user.LoginURL(c, r.URL.String())
  17. if err != nil {
  18. http.Error(w, err.Error(), http.StatusInternalServerError)
  19. return
  20. }
  21. w.Header().Set("Location", url)
  22. w.WriteHeader(http.StatusFound)
  23. return
  24. }
  25. fmt.Fprintf(w, "Hello, %v!", u)
  26. }

goapp serve输出中出现以下错误:

  1. (saucy)adam@localhost:~/projects/ringed-land-605/default$ goapp serve -host 0.0.0.0 .
  2. INFO 2014-06-08 23:57:47,862 devappserver2.py:716] Skipping SDK update check.
  3. INFO 2014-06-08 23:57:47,877 api_server.py:171] Starting API server at: http://localhost:48026
  4. INFO 2014-06-08 23:57:47,923 dispatcher.py:182] Starting module "default" running at: http://0.0.0.0:8080
  5. INFO 2014-06-08 23:57:47,925 admin_server.py:117] Starting admin server at: http://localhost:8000
  6. ERROR 2014-06-08 23:57:48,759 http_runtime.py:262] bad runtime process port ['hello46591\n']
  7. 删除`fmt.Print()`可以解决此问题。我的问题是为什么会发生这种情况?
  8. <details>
  9. <summary>英文:</summary>
  10. package helloworld
  11. import (
  12. &quot;fmt&quot;
  13. &quot;net/http&quot;
  14. &quot;appengine&quot;
  15. &quot;appengine/user&quot;
  16. )
  17. func init() {
  18. fmt.Print(&quot;hello&quot;)
  19. http.HandleFunc(&quot;/&quot;, handler)
  20. }
  21. func handler(w http.ResponseWriter, r *http.Request) {
  22. c := appengine.NewContext(r)
  23. u := user.Current(c)
  24. if u == nil {
  25. url, err := user.LoginURL(c, r.URL.String())
  26. if err != nil {
  27. http.Error(w, err.Error(), http.StatusInternalServerError)
  28. return
  29. }
  30. w.Header().Set(&quot;Location&quot;, url)
  31. w.WriteHeader(http.StatusFound)
  32. return
  33. }
  34. fmt.Fprintf(w, &quot;Hello, %v!&quot;, u)
  35. }
  36. Throw the following error in `goapp serve` output
  37. (saucy)adam@localhost:~/projects/ringed-land-605/default$ goapp serve -host 0.0.0.0 .
  38. INFO 2014-06-08 23:57:47,862 devappserver2.py:716] Skipping SDK update check.
  39. INFO 2014-06-08 23:57:47,877 api_server.py:171] Starting API server at: http://localhost:48026
  40. INFO 2014-06-08 23:57:47,923 dispatcher.py:182] Starting module &quot;default&quot; running at: http://0.0.0.0:8080
  41. INFO 2014-06-08 23:57:47,925 admin_server.py:117] Starting admin server at: http://localhost:8000
  42. ERROR 2014-06-08 23:57:48,759 http_runtime.py:262] bad runtime process port [&#39;hello46591\n&#39;]
  43. Removing the `fmt.Print()` fixes the issue. My question is why does that happen?
  44. </details>
  45. # 答案1
  46. **得分**: 4
  47. 在启动运行时进程时,[Go开发服务器](https://developers.google.com/appengine/docs/go/tools/devserver)(在App Engine Go SDK中)会读取在你的helloworld的`init`中找到的单行响应。
  48. 这会修改`http_runtime.py`中的`_start_process_flavor`标志;因此,HTTP运行时会尝试读取该行以确定监听哪个端口。
  49. &gt; 读取在启动进程文件中预期的单行响应。[...] START_PROCESS_FILE flavor 使用一个文件来报告运行时实例正在监听的端口。
  50. 在这种情况下,`hello`不是一个有效的监听端口。
  51. ----------
  52. 尝试使用Go的[`log`](http://golang.org/pkg/log/)包代替:
  53. log.Print("hello")
  54. <details>
  55. <summary>英文:</summary>
  56. 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`.
  57. 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.
  58. &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.
  59. In this case, `hello` isn&#39;t a valid port to listen on.
  60. ----------
  61. Try using Go&#39;s [`log`](http://golang.org/pkg/log/) package instead:
  62. log.Print(&quot;hello&quot;)
  63. </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:

确定