Go Programs featured image

Building and Installing Go Programs

Go is a statically typed programming language. Originally designed by Google, Go shares similarities with C in terms of syntaxes. However, in functionality, Go comes with additional features like memory safety, garbage collection, structural typing, etc. Over the past few years, Go has been gaining incredible popularity.

This guide will go over the steps of building and installing Go programs with the help of a demo Go program.

Go Building and Installation

Before jumping further, let’s have a quick breakdown of what building and installing means in the context of Go programming.

In Go programming, building refers to the process of translating the source code into a binary executable. The executable will contain the necessary support codes so that it can run independently on the target platform. Unlike Ruby/Python programs, Go binaries generally don’t require any system dependencies to run on a new system.

Putting the Go binary in an executable file path on the system is referred to as installing the program. As mentioned earlier, this guide will demonstrate the running, building, and installing of a simple Go “Hello world” program.

Prerequisites

Before starting, let’s go over the prerequisites you will need to fulfill to follow the tutorial:

First, we will quickly go over installing and configuring the Go programming environment. For a more in-depth step-by-step guide on installing Go check here.

Installing Go

Go is available on all the major platforms (Windows, Linux, and macOS). It’s officially available directly from Golang. Because of the website’s name, Go is often referred to as Golang. Here, we’ll always refer to the Go programming language as Go and the Golang website as Golang.

Golang offers detailed documentation on Go installation for all the supported platforms. However, the official method describes manual installation. Each time a new version is released, you have to manually update the installation to the latest version. Instead, we can directly install Go using APT (from default Ubuntu repos) or snap (from Snapcraft).

●     Installing Go using APT

Go is directly available from the official Ubuntu repos. However, it’s not always the latest version of Go. There are also two variations of Go available from the Ubuntu repo: golang-go and gccgo-go. While golang-go is the official version from Golang, gccgo-go is the Go frontend for GCC (as a part of GNU compiler). Generally, golang-go is the preferred choice.

To install golang-go, run the following APT command:

Go Programs apt update and go install

Check the Go version:

go version
●     Installing Go using snap

Snaps are universal Linux packages that can be installed on any supported distro. Snapcraft lists all the supported distros with installation guides. On Ubuntu, snap support is enabled by default. Go is also available as a snap. You need to keep in mind that it’s not officially maintained by Golang. The snap package is managed by a third-party provider.

To install Go snap, run the following command:

snap install go

Then, check the Go version:

snap go version

Setting up a Go Project

Our system has all the necessary tools to start developing apps in Go. Time to establish our demo project. First, create a dedicated directory for our project:

mkdir demo_project

Next, change the current directory to the newly-created directory:

cd demo_project

Here, create a new text file named main.go:

The following code features a simple program that will print “Hello world” when run. Copy the code:

Go Programs nano main_go

Save the file and close the editor. Verify that the code is functioning properly:

run main_go

The Go compiler built the source file into a binary file and executed it. It also verifies that the program is functioning successfully.

Go Workspace Configuration

In order to properly install Go programs, we need to configure Go workspace and GOPATH.

First, create the directory structure:

mkdir bin src

Next, we need to declare the value of GOPATH and add it to PATH. Open the bashrc file:

Go Programs nano bashrc

Add the following lines at the end of the file:

Go Programs nano bashrc modified

Reload bashrc:

Verify if the change was successful:

path variable

Building Go Binaries

This section will demonstrate compiling Go codes into a distributable standalone binary file. The binary will contain not only the Go code but also additional system codes to run the binary. To compile our code, run the following command:

build main_go

If there are multiple Go code files, then use the following command:

If no file is specified, Go will search for Go files in the current directory and compile all of them. Each binary file is named after its source Go file. In this example, Go built the program main. Run the program:

run main binary

This program is ready to be distributed to other computers.

●     Changing binary name

Whenever building, Go will automatically select a name for the executable file. There are two possible options when it comes to choosing the name.

  • If Go modules are used, then the executable will use the last part of the module’s name.
  • If no Go module is used, then Go will use the name of the current directory or the source file’s name.

Working with more complex programs requires a different naming convention. In such scenarios, these default naming methods aren’t very useful. In such a situation, the best way is to customize the output using the flag -o. Let’s have a quick demonstration. Build the Go program with the flag -o to designate a different filename:

build binary with different name

The -o flag tells Go to match the output with the given argument. In this case, the executable will be located under the directory bin and be named hello.

Next, test the new executable file:

bin hello_go

Installing Go Programs

So far, we’ve demonstrated building distributable Go binaries from source files. We can distribute them on other systems, deploy and perform tests. However, they can’t be executed outside of their source directories. If you’re planning to actively use the program, then this constraint can be very annoying. To make things easier, we can install the Go programs and access them from anywhere throughout the system.

To install the program, run the following Go command at the source directory:

go install

This command behaves the same way as go build. However, instead of leaving the executable file in the source directory, it’s placed into the following directory:

The variables GOPATH and GOBIN tell Go where to look for Go workspaces. GOPATH default is the location $HOME/go. We manually set GOBIN to $GOPATH/bin. To check the location of GOPATH and GOBIN use the following command:

go env path

Check the content of $GOPATH/bin. Our program should be there:

ls go path

Time to verify if the installation was successful. Change the current directory to a different location and try to run the Go program:

Go Programs check installation by running binary

Finally, you should be able to take your programs and install them into your system. You can use them from wherever and whenever you need them.

Final Thoughts

In this tutorial, we successfully demonstrated how to use Go toolchains to build executable binaries from source codes. These binaries can be used on other systems even without any Go configuration. Finally, we demonstrated how to install the Go program to be available from everywhere in the system.

For developers, CloudSigma offers Go API support to manage your cloud servers. It allows editing resources, and infrastructure scaling.

Happy Computing!