Build

A binary can also be built in a specific location using the go build command. A specific output file can be defined using the -o flag, otherwise it will be built in the working directory, using the package name as a binary name:

# building the current package in the working directory
$ go build .

# building the current package in a specific location
$ go build . -o "/usr/bin/mybinary"

When executing the go build command, the argument can be one of the following:

  • A package as a relative path (like go build . for the current package or go build ../name)
  • A package as an absolute path (go build some/package) that will be looked up in$GOPATH
  • A specific Go source file (go build main.go)

The latter case allows you to build a file that is outside $GOPATH and will ignore any other source file in the same directory.