【问题标题】:Google GO: routing request handling mystical declarations?Google GO:路由请求处理神秘声明?
【发布时间】:2012-09-06 18:31:52
【问题描述】:

我是第一次使用 Google GO。我已经扩展了“hello world”应用程序以尝试在 init 部分中定义路径。这是我到目前为止所做的:

package hello

import (
    "fmt"
    "net/http"
)

func init() {
    http.HandleFunc("/service", serviceHandler)
    http.HandleFunc("/site", siteHandler)
    http.HandleFunc("/", handler)
}

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello, there")
}

func serviceHandler( w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "this is Services")
}

func siteHandler( w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "this is Sites")
}

只有handler() 回调被执行——其他的被忽略。例如:http://myserver/service/foo 打印 Hello, there。我曾希望它是this is Services。

有没有更好的方法来进行服务路由?理想情况下,无论如何我都希望这些是单独的脚本,但看起来 Go 只有一个脚本,因为 app.yaml 需要在脚本声明中使用特殊字符串 _go_app。

谢谢!

【问题讨论】:

  • Go 没有脚本。它是一种编译语言。
  • 技术上,所有语言都被编译。但是,我们离题了。

标签: google-app-engine go


【解决方案1】:

根据文档位于:http://golang.org/pkg/net/http/#ServeMux

没有尾部斜杠的路径规范仅与该路径完全匹配。在末尾添加一个斜线,如下所示:http.HandleFunc("/service/", serviceHandler),它将按您的预期工作。

【讨论】:

  • 谢谢。我只是在谷歌开发者网站上关注 hello world,它没有提到 ServiceMux,所以我很感激参考。
猜你喜欢
  • 2018-07-19
  • 2018-07-18
  • 2021-12-18
  • 2012-12-01
  • 1970-01-01
  • 2015-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多