dongtang1909 2016-08-02 14:45
浏览 139
已采纳

使用go run的路径不适用于go install /从bin调用可执行文件

I'm starting to experiment with Go and I'm facing an issue that (I think) doesn't exist in languages that use a virtual machine.

I have a src/test/main.go file that references my templates inside src/test/views/ folder. When I use go run main.go it runs but when do go install and then inside my bin folder run the executable (./test) I get an error:

views/edit.html: no such file or directory

So, how does Go compiles my project (file/folder structure related) and how to use paths in a way that allows me to use either go run and go install/executable?

  • 写回答

1条回答 默认 最新

  • doushu5451 2016-08-02 15:03
    关注

    If you specify a relative path in your code, as in views/edit.html it will also be looked up relative to the binary location. You need to either make the path absolute, or use some logic to determine where your templates will be located.

    Another option would be to use https://github.com/jteeuwen/go-bindata or https://github.com/elazarl/go-bindata-assetfs that will save you the hassle.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘