【发布时间】:2015-03-12 18:38:09
【问题描述】:
main.go
package main
import (
"net/http"
)
func main() {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.ListenAndServe(":8080", nil)
}
目录结构:
%GOPATH%/src/project_name/main.go
%GOPATH%/src/project_name/static/..files and folders ..
即使在阅读了文档之后,我也很难理解 http.StripPrefix 在这里的作用。
1) 如果我删除了http.StripPrefix,为什么我无法访问localhost:8080/static?
2) 如果我删除该功能,哪个 URL 映射到 /static 文件夹?
【问题讨论】:
-
你试过http.Handle("/static/", http.FileServer(http.Dir("/")))吗?
-
http.Handle("/static/", http.FileServer(http.Dir("")))工作。
标签: http go url-rewriting server url-routing