doumei4964 2014-03-01 07:04
浏览 121
已采纳

http.Handle在Golang中不起作用

I try render a template:

func main() {
http.HandleFunc("/", index)
http.HandleFunc("/login", login)
err := fcgi.Serve(nil, http.HandlerFunc(handler))
}

func handler(w http.ResponseWriter, r *http.Request) {
   w.Header().Set("Content-type", "text/html")
   t, _ := template.ParseFiles("404.html")
   t.Execute(w, &page{Title: "not work"})
}

But when I open every page, even site.com/login, I see 404. Where can I find the problem?

  • 写回答

1条回答 默认 最新

  • duanpa2143 2014-03-01 07:15
    关注

    fcgi package documentation explains

    [...] If handler [second argument of fcgi.Serve] is nil, http.DefaultServeMux is used.

    In order to make use of functions registered with http.HandleFunc in http.DefaultServeMux you should not pass second argument to Serve function otherwise the handler function will serve all requests.

    func main() {
        http.HandleFunc("/", index)
        http.HandleFunc("/login", login)
        err := fcgi.Serve(nil, nil)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 请教一下c语言的代码里有一个地方不懂
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档