109

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?

Petr Razumov
  • 1,952
  • 2
  • 17
  • 32

2 Answers2

172
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.

Jossef Harush Kadouri
  • 32,361
  • 10
  • 130
  • 129
Craig Kelly
  • 3,776
  • 2
  • 18
  • 17
  • 13
    If you want to do it for a specific file `main.go` and you want to call it `myapp` run it like that: `go build -o myapp main.go`. – E235 Oct 05 '20 at 13:41
  • 1
    Maybe a noob question, but isn't this possible to set in code as a default. I have the case where a project has an `./example` dir to be installed. I'd like the binary name to be `-example` by default, without having to specify on cmd-line every time. Is the only way to set the package name accordingly? – Arnold Schrijver Jan 11 '21 at 20:12
  • 1
    I can't think of anything like that @ArnoldSchrijver - of the various special source options like build tags or the upcoming embedding, I don't think anything lets you change build tags from the source code. I suppose you might be able to hack something together with `go generate` side effects, but that seems like a path fraught with disaster. The easiest would probably just be a script that builds the examples the way you want. – Craig Kelly Jan 11 '21 at 22:21
5

How to specify an output name or path when building in Go (golang)

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

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265