gpt4 book ai didi

google-app-engine - 谷歌去 : routing request handling mystical declarations?

转载 作者:IT王子 更新时间:2023-10-29 00:57:08 24 4
gpt4 key购买 nike

我第一次使用 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

谢谢!

最佳答案

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

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

关于google-app-engine - 谷歌去 : routing request handling mystical declarations?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12306275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com