| <!--{ |
| "Title": "Compile and install the application", |
| "Path": "/doc/tutorial/compile-install" |
| }--> |
| |
| <p> |
| In the last section, you'll learn a new <code>go</code> command. While the |
| <code>go run</code> command is a useful shortcut for compiling and running a |
| single-file program, it doesn't generate a binary executable you can easily |
| run again. If you want one of those, a good choice is to run the |
| <a |
| href="https://golang.org/cmd/go/#hdr-Compile_and_install_packages_and_dependencies" |
| ><code>go install</code> command</a |
| >, which compiles your code and installs the resulting binary executable where |
| you can run it. |
| </p> |
| |
| <aside class="Note"> |
| <strong>Note:</strong> This topic is part of a multi-part tutorial that begins |
| with <a href="create-module.html">Create a Go module</a>. |
| </aside> |
| |
| <ol> |
| <li> |
| At the command line, change to the directory that contains hello/hello.go. |
| </li> |
| |
| <li> |
| Discover the Go install path, where the <code>go</code> command will install |
| the current package. |
| |
| <p> |
| You can discover the install path by running the |
| <a href="https://golang.org/cmd/go/#hdr-List_packages_or_modules" |
| ><code>go list</code> command</a |
| >, as in the following example: |
| </p> |
| |
| <pre> |
| go list -f '{{.Target}}' |
| </pre |
| > |
| |
| <p> |
| For example, the command's output might say |
| <code>/home/gopher/bin/hello</code>, meaning that binaries are installed |
| to /home/gopher/bin. This is the install directory you'll need in the next |
| step. |
| </p> |
| </li> |
| |
| <li> |
| Add the Go install directory to your system's shell path. |
| |
| <p> |
| That way, you'll be able to run your program's executable without |
| specifying where the executable is. |
| </p> |
| |
| <ul> |
| <li> |
| On Linux or Mac, run the following command: |
| |
| <pre> |
| export PATH=$PATH:/path/to/your/install/directory |
| </pre |
| > |
| </li> |
| |
| <li> |
| On Windows, run the following command: |
| |
| <pre> |
| set PATH=%PATH%;C:\path\to\your\install\directory |
| </pre |
| > |
| </li> |
| </ul> |
| |
| <p> |
| As an alternative, if you already have a directory like |
| <code>$HOME/bin</code> in your shell path and you'd like to install your |
| Go programs there, you can change the install target by setting the GOBIN |
| variable using the |
| <a href="https://golang.org/cmd/go/#hdr-Print_Go_environment_information" |
| ><code>go env</code> command</a |
| >: |
| </p> |
| |
| <pre> |
| go env -w GOBIN=/path/to/your/bin |
| </pre |
| > |
| |
| <p> |
| or |
| </p> |
| |
| <pre> |
| go env -w GOBIN=C:\path\to\your\bin |
| </pre |
| > |
| </li> |
| |
| <li> |
| Once you've updated the shell path, run the <code>go install</code> command |
| to compile and install the package. |
| |
| <pre> |
| $ go install |
| </pre |
| > |
| </li> |
| |
| <li> |
| Run your application by simply typing its name. |
| |
| <pre> |
| $ hello |
| map[Darrin:Hail, Darrin! Well met! Gladys:Great to see you, Gladys! Samantha:Hail, Samantha! Well met!] |
| </pre |
| > |
| </li> |
| </ol> |
| |
| <p> |
| That wraps up this Go tutorial! For a next step that introduces many more of |
| Go features, check out the |
| <a href="https://tour.golang.org/welcome/1">Tour of Go</a>. |
| </p> |
| |
| <p class="Navigation"> |
| <a class="Navigation-prev" href="add-a-test.html">< Add a test</a> |
| </p> |