How To Build and Install Go Program?
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
Step 2: Build the program
As you can see that in your directory there is a binary executable as per your OS.
Run the executable, On Unix systems
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
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:
Now you can take the programs you write and install them into your system, allowing you to use them wherever, whenever you need them.