How to Install and Use the Go Language on a Linux Cloud Server?

05-02-2024 02:00:54

The Go language, developed by Google, is an open-source programming language known for its simplicity, reliability, and efficiency. This guide is based on a Linux cloud server and outlines the procedure for installing and using the Go language, applicable to operating systems such as CentOS, Fedora, Ubuntu, Debian, and more.

Since the Go language installation package is often not the latest version in Debian's software repositories, it is recommended to download the latest version from the Go language's official website. Please replace "VERSION_NUMBER" in the following command with the actual version number.

$ cd /tmp
$ wget https://golang.org/dl/go1.<VERSION_NUMBER>.linux-amd64.tar.gz
$ sudo tar -C /usr/local -xzf go1.<VERSION_NUMBER>.linux-amd64.tar.gz

Before using the Go language, it's necessary to set some basic environmental variables.

$ echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.profile
$ echo "export GOPATH=~/.go" >> ~/.profile
$ source ~/.profile

To check the Go language version:

$ go version
# go version go1.<VERSION_NUMBER> linux/amd64

Below, we'll create a test code named helloworld.go to verify if the Go language environment is functioning properly.

$ nano helloworld.go

The content of the code is as follows:

package main
import (
    "fmt"
)
func main() {
    fmt.Println("Hello world!")
}

Run the code, and the result is as follows:

$ go run helloworld.go
# Hello world!