二进制和附加文件

huangapple go评论77阅读模式
英文:

Binary and additional files

问题

我正在尝试打包我的Go应用程序二进制文件,该文件通过在localhost:8080上运行的Web界面进行访问,以便在下载时能够在运行文件所在的文件夹中找到JS(前端)文件,但我似乎无法使其工作。

我一直在做这样的事情:

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

然后尝试使用二进制文件的工作目录来访问其中的文件,但似乎不起作用。

二进制文件位于:

/Users/admin/Desktop/testappfolder

但当我只运行以下代码时:

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

我得到的工作目录是/Users/admin

我想知道:

我做错了什么?
这与Gopath有关吗?
我在分发应用程序时是否以正确的方式进行操作,即将文件路径直接设置在程序中,相对于工作目录?还是逻辑有问题?

英文:

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

得分: 1

os.Getwd将对应于您启动二进制文件的位置,而不是二进制文件所在的位置。

为了实现更健壮的解决方案,我建议使用一个flag或配置值来传递文件目录的位置。

英文:

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.

huangapple
  • 本文由 发表于 2016年12月28日 10:46:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/41354700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定