Pau*_*mer 3 google-app-engine go
我是第一次使用谷歌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")
}
Run Code Online (Sandbox Code Playgroud)
只执行handler()回调 - 其他被忽略.例如:http://myserver/service/foo打印Hello, there.我曾希望会这样this is Services.
有没有更好的方法来进行服务路由?理想情况下,我希望这些都是单独的脚本,但看起来Go只有一个脚本,基于app.yaml _go_app在脚本声明中需要特殊字符串的事实.
谢谢!
根据以下文档:http://golang.org/pkg/net/http/#ServeMux
没有尾部斜杠的路径规范仅与该路径完全匹配.像这样在结尾添加一个斜杠:http.HandleFunc("/service/", serviceHandler)它将按预期工作.