Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build executable with name other than Golang package

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?

like image 983
Petr Razumov Avatar asked Mar 09 '17 21:03

Petr Razumov


People also ask

How do I create an executable in Golang?

Go The Go Command Go BuildPrintln("Hello, World!") } build creates an executable program, in this case: main or main.exe . You can then run this file to see the output Hello, World! . You can also copy it to a similar system that doesn't have Go installed, make it executable, and run it there.

Where does go build put binaries?

go install builds the command in a temporary directory then moves it to $GOPATH/bin .

How do you run a go binary?

The Go binaries are installed per default under /usr/local/go (Mac). You can verify the installation via the execution of the command go version in a fresh terminal. If go isn't already being found by the CLI you have to manually add /usr/local/go/bin to the PATH variable of your system.


1 Answers

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.

like image 66
Craig Kelly Avatar answered Sep 23 '22 21:09

Craig Kelly