Compile and install the application
In this last topic, you'll learn a couple new go commands. While
the go run command is a useful shortcut for compiling and running
a program when you're making frequent changes, it doesn't generate a binary
executable.
This topic introduces two additional commands for building code:
-
The
go buildcommand compiles the packages, along with their dependencies, but it doesn't install the results. -
The
go installcommand compiles and installs the packages.
-
From the command line in the hello directory, run the
go buildcommand to compile the code into an executable.$ go build
-
From the command line in the hello directory, run the new
helloexecutable to confirm that the code works.Note that your result might differ depending on whether you changed your greetings.go code after testing it.
-
On Linux or Mac:
$ ./hello map[Darrin:Great to see you, Darrin! Gladys:Hail, Gladys! Well met! Samantha:Hail, Samantha! Well met!]
-
On Windows:
$ hello.exe map[Darrin:Great to see you, Darrin! Gladys:Hail, Gladys! Well met! Samantha:Hail, Samantha! Well met!]
You've compiled the application into an executable so you can run it. But to run it currently, your prompt needs either to be in the executable's directory, or to specify the executable's path.
Next, you'll install the executable so you can run it without specifying its path.
-
On Linux or Mac:
-
Discover the Go install path, where the
gocommand will install the current package.You can discover the install path by running the
go listcommand, as in the following example:$ go list -f '{{.Target}}'For example, the command's output might say
/home/gopher/bin/hello, meaning that binaries are installed to /home/gopher/bin. You'll need this install directory in the next step. -
Add the Go install directory to your system's shell path.
That way, you'll be able to run your program's executable without specifying where the executable is.
-
On Linux or Mac, run the following command:
$ export PATH=$PATH:/path/to/your/install/directory
-
On Windows, run the following command:
$ set PATH=%PATH%;C:\path\to\your\install\directory
As an alternative, if you already have a directory like
$HOME/binin your shell path and you'd like to install your Go programs there, you can change the install target by setting theGOBINvariable using thego envcommand:$ go env -w GOBIN=/path/to/your/bin
or
$ go env -w GOBIN=C:\path\to\your\bin
-
On Linux or Mac, run the following command:
-
Once you've updated the shell path, run the
go installcommand to compile and install the package.$ go install
-
Run your application by simply typing its name. To make this interesting,
open a new command prompt and run the
helloexecutable name in some other directory.$ hello map[Darrin:Hail, Darrin! Well met! Gladys:Great to see you, Gladys! Samantha:Hail, Samantha! Well met!]
That wraps up this Go tutorial!