Open In App

How To Build and Install Go Program?

Improve
Improve
Like Article
Like
Save
Share
Report

Go (also known as Golang) is an open-source programming language focused on simplicity, reliability, and efficiency, developed at Google in 2007. It’s a multipurpose, statically typed, compiled programming language. It’s used for server-side (backend) programming, game development, cloud-native applications, and even Data Science. It is also popular for making command-line tools and is easy to learn. In Go translating source code into a binary executable is called building. go build built the package along with its dependencies into a binary executable, whereas go install first build then install the binary to your $GOPATH/bin folder. In other words, it installs the program into the system so that you can access it whenever or from wherever you need it.

Build and Install Go Program

Let’s see an example, follow the below steps to build and install the go program,

Step 1:  Create a program main.go and initialize the go.mod  file

go mod init gfg

Go




package main
    
import "fmt"
    
// Main function
func main() {
    fmt.Println("Welcome to GeeksforGeeks!")
    fmt.Println("Thanks for visiting us!")
}


Step 2: Build the program

go build

As you can see that in your directory there is a binary executable as per your OS.

Build-program

 

Run the executable, On Unix systems

./gfg

On windows,

gfg.exe

Running-executable

building the program

Now we’ve created a single executable binary that contains, not only your program but also all of the system code needed to run that binary.

Step 3: Install the program

go install

Installing-the-program

Installing the program

as you can see, the binary executable has been moved to the $GOPATH/bin directory. To check that executable, run the following commands,

go env GOPATH

ls $GOPATH/bin | grep gfg

Step 4: Verify the installation

Change the directory wherever you want and run that executable:

cd 

gfg

Verifying-the-installation

 

Now you can take the programs you write and install them into your system, allowing you to use them wherever, whenever you need them.


Last Updated : 02 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads