How to Install Go Programming Language on Debian 11
Go is a programming language that was initially created by a team at Google as a replacement for C. They wanted to have a language where the compiler would be quick but also have easy programing and efficient production. Go can be used for many things, like networking or distributed systems programs, and has earned the name “the language of the cloud”.
It helps people do more with just a few words, making it easier to write long sentences without wasting time on formatting. If you need to share your program with other people, you can just compile it into one file so they don't have to download anything.
In this tutorial, we'll look at how to install and configure a programming workspace with Go via command line. You can follow along if you have a working Debian 11 system. Other Debian-based Linux distributions should work in a similar way.
Prerequisite
Before you can get started with Go, make sure to have the following:
- A server with Debian 11 installed.
- A not-root user with sudo privileges.
Updating the System
Updating packages in Debian Linux is a quite basic process. To do so, you can just run the following command in your terminal:
sudo apt update && sudo apt upgrade -y
sudo apt install wget software-properties-common apt-transport-https
This will get all updates for your system and install any new packages that are available right now. You should usually start with this step before installing anything on your system.
Installing Go on Debian 11
In this step, you will download and install Go. The current release can be downloaded from the official Go downloads page.
As of writing, version 1.17 is the latest release. Find out which version is available for you, and replace the URL with the one that matches your current version.
cd
wget https://golang.org/dl/go1.17.linux-amd64.tar.gz
Once the download is complete, extract the downloaded ZIP and place it where you want it on your server. Keeping it on /usr/local is considered best practice:
sudo tar -zxvf go1.17.linux-amd64.tar.gz -C /usr/local/
Setting up the Go Environment
In this step we will configure the ${GOPATH} environment variable for GO.
You will need to create a local environment variable named $GOPATH and place it in your ~/.profile. For ease of development, $GOPATH instructed the compiler where to look for third-party source code as well as any code you had generated. Many third-party utilities still rely on this variable being set, thus setting it is still good practice even if it's not needed.
echo "export PATH=/usr/local/go/bin:${PATH}" | sudo tee /etc/profile.d/go.sh
source /etc/profile.d/go.sh
echo "export PATH=/usr/local/go/bin:${PATH}" | sudo tee -a $HOME/.profile source
source $HOME/.profile
To verify your settings have been applied correctly, run the echo command.
echo $PATH
This command should return a value similar to the following:
If this is not what you see with your terminal, repeat these steps ensuring that the values are accurate. If any command returns an error, ensure you have sourced your profile by executing source $HOME/.profile.
Now that you have your environment set up, you can verify the installation of Go by checking the version and the Go environment variables.
go version
go env
Creating a Testing Project
Now that you have Go installed, you can try creating a simple testing project. Let's create a "Hello World" project in our home directory.
Open your favorite text editor and create a new file called helloworld.go.
cd
sudo nano helloworld.go
Populate the file with the lines below.
package main
import "fmt"
func main() {
fmt.Println("Howtoforge, Hello World!")
}
Save the file with Ctrl+O and exit Nano with Ctrl+X.
Now you can go ahead and run your new project with the go run command.
go run helloworld.go
The fmt, an abbreviation for Format package, will call the Println function. The Println function will display values to the console. As a result, in your terminal you'll see the "Howtoforge, Hello World!" message.
This output confirms that the Go workspace is working correctly.
Conclusion
In this tutorial, we have shown you how to get started with Golang on Debian 11. Go is a powerful language with many tools and an excellent community. You can find more information about Go on its official website.
If you have any questions, feel free to leave comments below. If you liked our guide, share it with your friends by clicking on the social media buttons.