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.
Before getting started, make sure you have
A Vultr CentOS 8, Ubuntu 18.04, or Debian 10 VPS instance
A user with root privileges
Log in to your Vultr instance via SSH.
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 tar
to the /usr/local
path.
Remove the downloaded package.
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 -f
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 -f
At 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.
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/profile
Now, let's verify the Go setup.
go version
This 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.
Let's write the Hello World application to test our setup.
Create a folder and navigate into it.
mkdir hello
cd hello
Create a Go module using the go mod
command.
go mod init hello
Create a file named hello.go
touch hello.go
Edit the file hello.go
.
vi hello.go
Once 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.go
You should see the output:
Hello World!
You have successfully set up Go and written your first application.