Introduction
Go, sometimes referred to as “Golang”, is an open-source programming language that was released by Google in 2012. Google’s intention was to create a programming language that could be learned quickly.
Since its launch, Go has become very popular among developers and is used for various applications ranging from cloud or server-side applications, to artificial intelligence and robotics. This tutorial describes how to download and install the latest version of Go (currently version 1.16.7) on an Ubuntu 20.04 server, build the famous Hello, World! and convert your Go code into an executable binary for future use.
Prerequisites
This tutorial requires an Ubuntu 20.04 system configured with a
non-root user with sudo privileges and a firewall as described in Initial Server Setup with Ubuntu 20.04.
Step 1 — Install
Go
In this step, you will install Go on
your server. First, connect to your
Ubuntu server via ssh:
- ssh sammy@your_server_ip
Next, navigate to the official Go download page in your web browser. From there, copy the tarball URL of the current binary version.
At the time of writing, the latest version is go1.16.7. To install Go on an Ubuntu server (or any Linux server, for that matter), copy the URL of the file ending with linux-amd64.tar.gz.
Now that you
have your link ready, first confirm that it is in the home directory:
- cd ~
Then use curl to retrieve the tarball, making sure to replace the highlighted URL with the one you just copied. The -O flag ensures that this is sent to a file, and the L flag indicates HTTPS redirects, as this link was taken from the Go website and will redirect here before the file is downloaded
:
- curl -OL https://golang.org/dl/go1.16.7.linux-amd64.tar.gz
To verify the integrity of the file you downloaded, run the sha256sum command and pass it to the filename as an argument:
sha256sum go1.16.7.linux-amd64.tar.gz
This will return
the SHA256 checksum of the tarball: Outputgo1.16.7.linux-amd64.tar.gz 7fe7a73f55ba3e2285da36f8b085e5c0159e9564ef5f63ee0ed6b818ade8ef04
- go1.16.7.linux-amd64.tar.gz
If the checksum matches the one listed on the download page, you have completed this step successfully
.
Then use tar to remove the tarball. This command includes the -C flag that instructs tar to change to the given directory before performing any other operation. This means that the extracted files will be written to the /usr/local/ directory, the recommended location to install Go… The x prompt tells tar to extract, v tells you that we want a detailed output (a list of the files that are extracted), and f tells you that we will specify a file name:
sudo tar -C /usr/local –
- xvf go1.16.7.linux-amd64.tar.gz Although /usr/local/
go is the recommended location to install Go, some users may prefer or require different paths.
Step 2 — Configure Go routes
In this step, It will establish routes in its environment.
First, set the root value of Go, which tells Go where to look for your files. To do this, edit the .profile file, which contains a list of commands that the system executes each time you log on.
Use your preferred editor to open .profile, which is stored in the user’s home directory. Here, we will use
nano:
- sudo nano~/.profile
Then, add the following information to the end of your file:
. . . export PATH=$PATH:/usr/local/go/bin
After you add this information to your profile, save and close the file. If you used nano, do so by pressing CTRL + X, then Y, and then ENTER.
Then update your profile by running the
following command
:
- source ~
/.profile
Next, check if you can run go commands
by running go version: go version This
command will generate the version number of any version of Go installed on your system:
OutputGo
- version
go1.16.7 linux/amd64
This output confirms that you are now running Go on your server.
Step 3 — Test the installation
Now that Go is installed and the paths are set up for your server, you can try creating your Hello, World! to make sure Go is working.
First, create a new directory for your Go workspace, which is where Go will build your files
:
- mkdir
hello
Then move to the directory you just created:
- cd hello
When importing packages, you need to manage dependencies through the code module itself. You can do this by creating a go.mod file with the command
go mod init:
- go mod init your_domain/hello
Next, create a Hello, World! Go file in your preferred text editor
: nano hello.go
Add the following text to your hello.go file:
package main import “fmt” func main() { fmt
- .
Println(“Hello, world!”) }
Then, save and close the file by pressing CTRL+X, Y, and then ENTER.
Test your code to verify that it prints the Hello, World!:
- go run
greeting. DepartureHello, world!
The go run command compiles and runs the Go package from a list of .go source files in the new hello directory you created and the path you imported. But, you can also use go build to create an executable file that can save you some time.
Step 4 — Convert your Go code into a binary executable
The go run command is typically used as a shortcut to compile and run a program that requires frequent changes. In cases where you have finished your code and want to run it without compiling it each time, you can use go build to convert the code into an executable binary. Creating code in an executable binary consolidates the application into a single file with all the supporting code needed to run the binary. Once you’ve created the binary executable, you can run go install to place your program in an executable file path so you can run it from anywhere on your system. Your program will then correctly print Hello, World! when prompted and you will not need to compile the program again.
Try it and run go build. Be sure to run this from the same directory where your hello.go file is stored
:
- go build
Then run ./hello to confirm that the code is working correctly: .
- /Hello
DepartureHello, world!
This confirms that you have successfully converted your hello.go code into an executable binary. However, you can only invoke this binary from this directory. If you want to run this program from a different location on your server, you will need to specify the full binary file path to run it.
Writing the full file path of a binary can quickly become tedious. Alternatively, you can run the go install command. This is similar to go build, but instead of leaving the executable in the current directory, go install places it in the $GOPATH/bin directory, allowing you to run it from anywhere on your server.
To run go install successfully, you must pass it the installation path of the binary you created with go build. To find the installation path of the binary, run the following command
go list:
- go list -f ‘{{. The
destination go list}}
generates a list of named Go packages stored in the current working directory. The f flag will cause the go list to return the output in a different format depending on the package template passed to it. This command prompts you to use the Target template, which will cause go list to return the installation path of any packages stored in this directory:
Output’/home/sammy/go/bin/hello
This is the installation path of the binary file you created with go build. This means that the directory where this binary is installed is /home/sammy/go/bin/.
Add this installation directory to the system shell path. Be sure to change the highlighted part of this command to reflect the binary installation directory on your system, if different
:
- export PATH=$PATH:/home/sammy
/go/bin/ Finally, run go install to
compile and install the package:
- go to install
Try running this executable binary by simply entering
- hello
OutputHello, World!
If you received the Hello, World! output, you have made your Go program correctly executable from a specific, unspecified path on your server.
Conclusion
By downloading and installing the latest Go package and setting your routes, you now have a system to use for Go development. You can find and subscribe to additional articles about installing and using Go within our “Go” tag.