duanpenpan5796 2014-12-11 05:42
浏览 7
已采纳

如何访问软件包外部的模板?

I have setup a simple website based off of this structure. I run main.go and everything works fine. I'd like to be able to use this webapp in another package, say "github.com/my/package". I copied "main.go" to the "github.com/my/package" directory and run it but then get:

"panic: open templates/user/view.html: no such file or directory"

What is the recommended way to modify the path to the template file in this file, for instance, so that I can access the templates? I can think of two solutions:

  1. Hardcode an absolute path in view.go to the template file.
  2. Have a global variable in view.go then figure out where the template files are in relation to the new main.go file & set the variable to that path.

The first will obviously break if someone else were to try to use the package. The second option is a bit convoluted b/c you need to find the path to main then work your way through & figure out where the templates are...seems very convoluted.

Is there a better way?

  • 写回答

2条回答 默认 最新

  • douzhi3454 2014-12-11 15:56
    关注

    you could look at go-bindata. It makes external files, like templates, as part of the binary. Although it's not a great solution if you want to be able to change the templates without recompiling

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

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