我有这个目录结构,我使用Gorilla mux:
目录结构
twitter
layout
stylesheets
log.css
log.html
twitter.go
按照这里的建议:http://www.shakedos.com/2014/Feb/08/serving-static-files-with-go.html我这样做了:
var router = mux.NewRouter()
func ServeStatic(router *mux.Router, staticDirectory string) {
staticPaths := map[string]string{
"styles": staticDirectory + "stylesheets",
}
for pathName, pathValue := range staticPaths {
pathPrefix := "/" + pathName + "/"
router.PathPrefix(pathPrefix).Handler(http.StripPrefix(pathPrefix,
http.FileServer(http.Dir(pathValue))))
}
}
var staticDirectory = "/layout/"
func main() {
(//other code)
ServeStatic(router, staticDirectory)
}
我仍然无法链接CSS文件。我做错了什么?
答案 0 :(得分:6)
解决。
我在func main()
中添加了这个router.PathPrefix("/").Handler(http.FileServer(http.Dir("./layout/")))
答案 1 :(得分:0)
您可以更轻松地执行此操作,而无需在main()中添加额外的行:
在ServeStatic内: 添加:“。”+在pathValue
之前router.PathPrefix(pathPrefix).Handler(http.StripPrefix(pathPrefix,
http.FileServer(http.Dir("."/pathValue))))