How to build executable with name other than Golang package

GoBuildPackageNaming Conventions

Go Problem Overview


Is it possible to build (install, go get, etc) an executable with the name foobar if my Golang package name is one of the following:

  • github.com/username/go-foobar
  • github.com/username/foobar-tools

and has main.go in the package root?

Go Solutions


Solution 1 - Go

go build -o <your desired name>

You can specify the executable name using the -o switch with go build. For your example it would look something like: cd $GOPATH/github.com/username/go-foobar && go build -o foobar. However, you're just left with the executable in the package's folder -- you still need to install it somehow.

However, I don't know of any way to specify that for someone using go get github.com/username/go-foobar to install your tool. For instance, see this answer: https://stackoverflow.com/a/33243591/2415176

If you're not worried about people installing your tool with go get, this is the kind of thing you can wrap in a Makefile.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPetr RazumovView Question on Stackoverflow
Solution 1 - GoCraig KellyView Answer on Stackoverflow