亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Go:使用 gorilla mux 提供 CSS 文件

Go:使用 gorilla mux 提供 CSS 文件

Go
九州編程 2021-08-10 15:38:40
我有這個目錄結構,我正在使用 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/")))


查看完整回答
反對 回復 2021-08-10
?
飲歌長嘯

TA貢獻1951條經驗 獲得超3個贊

您可以通過更簡單的方式完成此操作,而無需在 main() 中添加額外的行:

在 ServeStatic 中:在 pathValue 之前添加:“.”+

router.PathPrefix(pathPrefix).Handler(http.StripPrefix(pathPrefix,
            http.FileServer(http.Dir("."/pathValue))))


查看完整回答
反對 回復 2021-08-10
  • 2 回答
  • 0 關注
  • 220 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號