dqkyz02602 2019-02-21 15:03
浏览 32
已采纳

图片文件夹的更短路径-Golang

I have a solution that works, but I want to fine tune it and at the same time understand how it works.

Here is my folder structure:

web/
├── main.go
└── public/
      ├── css/
      ├── js/
      ├── img/
      │    └── pict.jpg
      └── templates/

The current path that works to get a picture is:

<img src="public/img/pict.jpg"></a>

The desired path to get a picture is (I want to skip the public/ part):

<img src="img/pict.jpg"></a>

The relevant go code:

func main() {
    http.HandleFunc("/", index)
    http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("./public"))))
    http.ListenAndServe(":8080", nil)
}

The same short path should also apply on css and js folder. TIA!

  • 写回答

1条回答 默认 最新

  • dousong8187 2019-02-21 15:37
    关注

    Try this: downside is you need to repeat this for each sub-directory

    http.HandleFunc("/", index)
    
    http.Handle("/img/",
        http.StripPrefix("/img/", http.FileServer(http.Dir("./public/img"))))
    http.Handle("/css/",
        http.StripPrefix("/css/", http.FileServer(http.Dir("./public/css"))))
    
    // ... etc.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度