【发布时间】:2018-01-14 09:57:20
【问题描述】:
你好,很棒的 stackoverflow 社区,
为这个蹩脚的问题道歉。
我一直在玩 Go 中的 net/http 包,并试图设置一个 http.Handle 来提供目录的内容。我的句柄代码是
func main() {
http.Handle("/pwd", http.FileServer(http.Dir(".")))
http.HandleFunc("/dog", dogpic)
err := http.ListenAndServe(":8080", nil)
if err != nil {
panic(err)
}
}
我的 dogpic 处理程序正在使用 os.Open 和 http.ServeContent,工作正常。
但是,当我尝试浏览 localhost:8080/pwd 时,我得到了一个 404 页面,但是当我将模式更改为路由到 / 时,
http.Handle("/", http.FileServer(http.Dir(".")))
它正在显示当前页面的内容。有人可以帮我弄清楚为什么fileserver 不能与其他模式一起使用,而只能与/ 一起使用吗?
谢谢。
【问题讨论】:
标签: http go http-status-code-404 handler