Go (also known as Golang) is a statically-typed, compiled, C-like programming language developed by Google. Go's simplicity and versatility has made it to be the preferred language for developing high-performance web applications and microservices.
Go can be installed in both 32-bit & 64-bit Linux Operating Systems. Though these steps are written for CentOS, Ubuntu, Debian, and Fedora, it is applicable for any Linux distribution.
Prerequisites
Before getting started, make sure you have
- A Rcs CentOS 8, Ubuntu 18.04, or Debian 10 VPS instance
- A user with root privileges
Installation
Log in to your Rcs instance via SSH.
Step 1: Download and Unzip Go 1.13 archive
Go's build, runtime, and language-support tools are available as a TAR archive for Linux. The installation process:
- Download the archive using
wget. - Extract it using
tarto the/usr/localpath. - Remove the downloaded package.
Example commands for 64-bit systems
wget https://dl.google.com/go/go1.13.6.linux-amd64.tar.gz
sudo tar -zxvf go1.13.6.linux-amd64.tar.gz -C /usr/local
rm go1.13.6.linux-amd64.tar.gz -fExample commands for 32-bit systems
wget https://dl.google.com/go/go1.13.6.linux-386.tar.gz
sudo tar -zxvf go1.13.6.linux-386.tar.gz -C /usr/local
rm go1.13.6.linux-386.tar.gz -fAt the time of writing this guide, the latest available version was 1.13. You can check the latest Go version from the Go official download page.
Step 2: Setup Environment variables
The Go's runtime and build executables are now available under /usr/local/go/bin.
Add the executable path to PATH environment variable.
Add the GOROOT environment variable referencing your local Go installation.
Use thesource command to reload the updated values.
echo 'export GOROOT=/usr/local/go' | sudo tee -a /etc/profile
echo 'export PATH=$PATH:/usr/local/go/bin' | sudo tee -a /etc/profile
source /etc/profileStep 3: Verification
Now, let's verify the Go setup.
go versionThis should print out the current version.
go env This should print out all the flags. If so, you have set up the Go installation in your system successfully.
OPTIONAL: A quick Hello World program
Let's write the Hello World application to test our setup.
Create a folder and navigate into it.
mkdir hello cd helloCreate a Go module using the
go modcommand.go mod init helloCreate a file named
hello.gotouch hello.goEdit the file
hello.go.vi hello.goOnce inside the vi editor, press I to switch to 'Insert' mode
Enter the following code snippet in the editor.
package main import "fmt" func main() { fmt.Printf("Hello World!") }
Executable Go programs start with package main. We imported the fmt package, which provides methods to print text.
Save the file by pressing Esc and type WQEnter
Run your first Go application
go run hello.goYou should see the output:
Hello World!
You have successfully set up Go and written your first application.