douman6245 2016-12-28 02:46
浏览 7
已采纳

二进制文件和其他文件

I'm trying to package my go application binary which is accessible by a web interface running on localhost:8080 so that when downloaded it's able to find the JS(front-end) files in the folder where the file is ran but i can't seem to make it work.

I've been doing something like this :

  pwd, err := os.Getwd()
  if err != nil {
      fmt.Println(err)
      os.Exit(1)
  }

Then trying to use the working directory of the binary to access the files inside it but that doesn't seem to work.

The binary is located at :

/Users/admin/Desktop/testappfolder

but when i run the program with just :

pwd, err := os.Getwd()
if err != nil {
    fmt.Println(err)
    os.Exit(1)
}
fmt.Println(pwd)

I end up getting /Users/admin as the working directory instead.

I wondering :

Where i'm going wrong ? Is this has something to do with the Gopath ? Am i going at it the right way regarding distributing the app as a "zip" and having file path setup directly inside my program relative to the working directory ? or is it that logic that's wrong ?

  • 写回答

1条回答 默认 最新

  • dongzhuang1923 2016-12-28 04:49
    关注

    os.Getwd is going to correspond to where you start your binary from not where the binary is located.

    To make for a more robust solution I would pass in the location of the files directory using a flag or using a config value.

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

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致