← back

Go output filenames

2026-07-21

Ever wondered how Go chooses the executable name when building? I have previously erroneously assumed that it is derived from the basename of the working directory:

go build -o out/
./out/"$(basename "$0")"

This is incorrect! It actually outputs based on the module path, which is defined in go.mod. You can use go list -m to output the module name in the current working directory. So the right way to do it is this:

go build -o out/
./out/"$(basename "$(go list -m)")"

Of course, you can simply hard code the value in here, but this way is copyable across your go projects. It also helps when you’re working with git workflows, since those are typically stored in folders with a different name than your original branch.