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?
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 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.
For anyone landing here wondering how to build single Go files with specific executable names or into specific output directories, here's how:
# ---------------------
# general formats
# ---------------------
# 1. build input file "filename.go" into an executable
# named "executable_filename" within the directory you are currently `cd`ed
# into
go build -o executable_filename filename.go
# 2. build input file "filename.go" into an executable at
# path "output_dir/filename"; this automatically creates directory "output_dir"
# if it does not already exist (don't forget the trailing slash after the
# directory name!)
go build -o output_dir/ filename.go
# 3. build input file "filename.go" into an executable at
# path "output_dir/whatever"; this automatically creates directory "output_dir"
# if it does not already exist
go build -o output_dir/whatever filename.go
# ---------------------
# examples
# ---------------------
# create executable "whatever" from "hello_world.go"
go build -o whatever hello_world.go
# make directory "bin" and create executable "bin/hello_world"
# from "hello_world.go"
go build -o bin/ hello_world.go
# make directory "bin" and create executable "bin/whatever"
# from "hello_world.go"
go build -o bin/whatever hello_world.go
# ---------------------
# help
# ---------------------
# see the short help menu for `go build`
go build --help
# see the long help menu for `go build`
go help build
Note that the -o whatever
must always come before the input filenames! It is an error to do otherwise.
From go build --help
, you can see that -o output
must come before build flags and packages:
usage: go build [-o output] [build flags] [packages] Run 'go help build' for details.
For more details, see go help build
.
I learned all of the above from the above documentation and my own experimentation and trial and error.
You can try the above commands on my Go examples in my eRCaGuy_hello_world repo here: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/tree/master/go