Cam.phiefr
Cam.phiefr

Reputation: 97

Binary and additional files

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 ?

Upvotes: 0

Views: 49

Answers (1)

jmaloney
jmaloney

Reputation: 12270

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.

Upvotes: 1

Related Questions