VOOZH about

URL: https://thenewstack.io/golang-how-to-use-the-go-install-command/

⇱ Golang: How To Use the Go Install Command - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2024-05-01 17:00:48
Golang: How To Use the Go Install Command
Go

Golang: How To Use the Go Install Command

Want to run your Go program as a full executable binary? The Go install command compiles and installs the application in your workspace's bin directory. Here's how.
May 1st, 2024 5:00pm by Jack Wallen
👁 Featued image for: Golang: How To Use the Go Install Command
Feature image by Gerd Altmann from Pixabay.

The Go language has a special command that is used to compile and install a binary package for your application into a path that is accessible to your application’s user.

Let me explain it in a way we can all understand.

First, let’s talk about the PATH. A PATH is a special list of directories that instructs the system where to find the necessary executable files to run commands. For instance: With Linux, run the command…

echo $PATH

You’ll probably see something like this in the output:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Essentially, what that means is any executable file in any of those directories can be run from anywhere in the filesystem hierarchy. Thanks to the PATH, you don’t have to use the full path to commands like ls, which would be:

/usr/bin/ls

Instead, you can simply run ls to use the application.

When you install Go, it assumes the path for Go defaults to a particular location. To find out where that path is, issue the command:

echo $GOPATH

You should see something like this:

/home/jack/go/

You can set this path with the command:

go env -w GOPATH=$HOME/go

Note that $HOME is the equivalent of /home/USER (Where USER is your Linux username). You can also set this in your .bash_profile file. Open that file for editing with the command:

nano ~/.bash_profile

At the bottom of that file, add the following:

export GOPATH=$HOME/go

Source the file with:

source ~/.bash_profile

You could change that path if you wanted to, but it’s always best (especially at first), to leave it as is.

Okay, now that you understand what GOPATH is, how is it used?

Let me show you.

Let’s write a program that will calculate the approximate value of Pi. Here’s how this application works:

  1. Imports the packages fmt, math, and math/rand.
  2. Seeds the random number generator, sets totalPoints to 1 million and pointsInsideCircle to 0.
  3. Uses a for loop to iterate through totalPoints, setting both x and y to random float 64 numbers and uses those numbers (with the math.Sqrt function) to multiple x*x and y*y.
  4. Sets piApprox to 4 times float64 of pointsInsideCircle and totalPoints.
  5. Finally, prints out the value.

Here’s what the code looks like:

Create a new project directory with the following:

mkdir ~/randompi

Change into that directory with the following:

cd randompi

Initialize the project with:

go mod init randompi

Create the main.go file with:

nano main.go

Paste the code into that file and save it.

Build the application with the command:

go build

What you should now see is a binary executable called randompi. You can run the new Go app with the command:

./randompi

That’s great. But what if you wanted to be able to run that command from other directories? Since this is Linux, you could copy that to the /usr/local/bin directory but Go already has its GOPATH available for that very purpose. For this, you use go install, which moves that new binary file to your GOPATH. To do that, issue the command:

go install

If you issue the ls command, you’ll find that randompi executable is now gone. Where did it go? Go moved it into your GOPATH. Remember to list your GOPATH with:

echo $GOPATH

That should print out your GOPATH. The trick here is that Go doesn’t just copy the executable to the root of your GOPATH. Instead, it copies it to the bin directory within that path. In Linux terms, bin is a common directory for binary files (bin = binary). To verify the executable was copied into that path, issue the command:

ls $GOPATH/bin

You should see randompi listed.

If you know Linux, you probably understand what’s coming next. Even though you’ve set GOPATH, that doesn’t mean it’s in your Linux PATH. Even with that caveat, Go has you covered with the run command. If you issue the command:

go run randompi

It will find the binary executable in $GOPATH/bin and run the randompi application for which the output will be something like:

Using this method, our approximate value of π: 3.139740

Here’s a handy trick.

When you initialized the application with go mod init randompi, it creates the go.mod file which will include something like this:

module randompi

go 1.22.1

Say you want to rename the app to something like gopi. All you have to do is edit the go.mod file and change the module randompi to module gopi. Rebuild and reinstall the app and you can then run your app with:

go run gopi

And that, my Go friends is the basics for using the go install command. This will become an important tool for you as you continue your education in the Go language.

TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.