0

Using Go on Windows, whenever I execute go run myprog.go from the console, Go builds a new executable with a random name somewhere on my C drive.

Is there a way to configure it so it will always build the file to a specific location, and preferably to also avoid the randomness of the name? (i.e., always build to D:\Temp\LastBuild.exe).

I was able to find some info that did not help when doing go help run and go help build. The latter had an output flag -o outfile but it is not accepted in go run.

Any help is appreciated.

1 Answer 1

2

Do not use go run. It's intended for quick testing out snippets of code the size of a single screenful of lines.

The working approach is

  1. Edit the code.
  2. go build it.
  3. Run your executable which will have predictable name.
  4. Go to step (1).

If you write tests in parallel with the implementation (and you should), this changes to

  1. Edit the code.
  2. Edit the test suite.
  3. go test it.
  4. Go to step (1).

Please see this recent thread on the mailing list for more insight on the same subject.

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

3 Comments

Ok - I erased my last comment, I take it back. I have changed my sublime build system to run go build -o D:/Dev/Go/debug.exe && D:/Dev/Go/debug.exe instead of go run $file_name - which gets me the result I needed without losing anything. Thanks.
@DannyB, If you're using Sublime, consider using GoSublime with it: it has all the necessary stuff for integrating Sublime's own build system with the go tool.
thanks, I am using Sublime and the excellent GoSublime. But I am not crazy about its build system. So I built my own. The truth is that I would still love to have the ability to change where go run and go test build their binaries.

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.