我有這個目錄結構,我正在使用 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 文件。我究竟做錯了什么?
2 回答

慕田峪9158850
TA貢獻1794條經驗 獲得超8個贊
解決。
我在 func main() 中添加了這個
router.PathPrefix("/").Handler(http.FileServer(http.Dir("./layout/")))

飲歌長嘯
TA貢獻1951條經驗 獲得超3個贊
您可以通過更簡單的方式完成此操作,而無需在 main() 中添加額外的行:
在 ServeStatic 中:在 pathValue 之前添加:“.”+
router.PathPrefix(pathPrefix).Handler(http.StripPrefix(pathPrefix, http.FileServer(http.Dir("."/pathValue))))
- 2 回答
- 0 關注
- 220 瀏覽
添加回答
舉報
0/150
提交
取消