4

In Go compiler, when I do "go run", executable file is stored to a temporary location. How to change this path to store the file in current working directory? I am using windows 7 64bit machine.

2
  • 2
    The intention of go run is to execute application without storing of final executable on disk. That's the reason, it generates executable in temporary location and once done, file is ready to be disposed off. Commented Oct 4, 2017 at 17:20
  • 2
    go run just executes go build -o with a temp path, executes the binary, then deletes it. If you don't want to do that, then don't use go run. Commented Oct 4, 2017 at 21:51

4 Answers 4

9

If you want to do something with the binary beyond just running it once, you should be using go build, not go run. go build will put the binary in the current working directory.

Sign up to request clarification or add additional context in comments.

Comments

5

The folder used by Go to store the temporary executable can be changed by setting the GOTMPDIR environment variable.

More info here: https://stackoverflow.com/a/71197493/1057961

Comments

2

I agree with @Adrian and @Saleem, however, for interest sake, you can override the location (somewhat) by changing the location of your environment variable TEMP (or TMPDIR on OSX or Linux). This will still create a temporary directory in whatever directory you specify, in which the working files will be placed. Keep in mind that as Adrian and Saleem say, go run is intended for temporary runs.

And of course @JimB beat me to it with his comment which is really the essence of what I'm saying here.

Comments

2

Agree with previous answers. go install saves binary file into GOBIN folder. So you may change it to have a specific location. However, I don't suggest to do it, because you can always build into specific folder using -o option of go build:

go build -o /usr/bin/app main.go

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.