dongrong9053 2012-09-06 18:31
浏览 58
已采纳

Google GO:路由请求处理神秘的声明?

I'm dorking around with Google GO for the first time. I've extended the "hello world" application to try to have paths defined in the init section. Here's what I've done so far:

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")
}

Only the handler() callback is ever executed -- the others are ignored. E.g.: http://myserver/service/foo prints Hello, there. I had hoped that it would be this is Services.

Is there a better way to do service routing? Ideally, I would expect these to be separate scripts anyway, but it looks like Go has only one script, based on the fact that the app.yaml requires a special string _go_app in the script declaration.

Thanks!

  • 写回答

1条回答 默认 最新

  • donglu6303 2012-09-06 18:51
    关注

    According to the documentation at: http://golang.org/pkg/net/http/#ServeMux

    path specs that do not have a trailing slash only match that path exactly. Add a slash to the end like so: http.HandleFunc("/service/", serviceHandler) and it will work as you expect.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看