如何在同一路径中设置主页和静态文件
How to set Home page and static files in same path
如果我 运行 URL http://localhost:8080/ 我想 运行 索引函数 如果我 运行 http://localhost :8080/robot.txt 它应该显示静态文件夹文件
func main() {
http.HandleFunc("/", Index)
http.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir("static"))))
http.ListenAndServe(":8080", nil)
}
func Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Index")
}
如何操作。
目前,我收到此错误
panic: http: multiple registrations for /
这是我的目录结构
main.go
static\
| robot.txt
| favicon.ico
| sitemap.xml
从索引处理程序委托给文件服务器:
func main() {
http.HandleFunc("/", Index)
http.ListenAndServe(":8080", nil)
}
var fserve = http.StripPrefix("/", http.FileServer(http.Dir("static"))))
func Index(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != “/“ {
fserve.ServeHTTP(w, r)
return
}
fmt.Fprintln(w, "Index")
}
如果我 运行 URL http://localhost:8080/ 我想 运行 索引函数 如果我 运行 http://localhost :8080/robot.txt 它应该显示静态文件夹文件
func main() {
http.HandleFunc("/", Index)
http.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir("static"))))
http.ListenAndServe(":8080", nil)
}
func Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Index")
}
如何操作。
目前,我收到此错误
panic: http: multiple registrations for /
这是我的目录结构
main.go
static\
| robot.txt
| favicon.ico
| sitemap.xml
从索引处理程序委托给文件服务器:
func main() {
http.HandleFunc("/", Index)
http.ListenAndServe(":8080", nil)
}
var fserve = http.StripPrefix("/", http.FileServer(http.Dir("static"))))
func Index(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != “/“ {
fserve.ServeHTTP(w, r)
return
}
fmt.Fprintln(w, "Index")
}