How To Set up Golang on Debian 11

In this article, we want to teach you How To Set up Golang on Debian 11.

Golang is an open-source web development language that was created by Google.

This language has gained immense fame because of its top-notch security, concurrency, and user-friendliness.

Golang combines the notable performance and top security features of C++ with the speed of Python.

Set up Golang on Debian 11

Before you start to install Golang on Debian 11, you need to log in to your server as a non-root user with sudo privileges. To do this, you can follow our article the Initial Server Setup with Debian 11.

Now follow the steps below to install Golang on Debian 11.

Install Golang on Debian 11

First, you need to update your local package index with the command below:

sudo apt update

Then, you need to visit the Go Downloads page to check the latest version of Golang. Then, install the wget tool on your server to download the Go:

sudo apt install wget -y

Now use the following command to download Golang on your Debian 11:

wget https://golang.org/dl/go1.17.linux-amd64.tar.gz

Extract your Golang downloaded file to the /usr/local directory with the command below:

sudo tar -zxvf go1.17.linux-amd64.tar.gz -C /usr/local/

At this point, you need to set the path environment variable to include Go’s bin directory. To do this, run the following command:

echo "export PATH=/usr/local/go/bin:${PATH}" | sudo tee /etc/profile.d/go.sh

Next, you need to source the file with the command below:

source /etc/profile.d/go.sh

Verify your Golang installation on Debian 11 by checking its version:

go version

In your output you will see:

Output
go version go1.17 linux/amd64

Also, you can check the Golang environment variables on Debian 11:

go env

In your output you will see:

Output
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmess age-length=0 -fdebug-prefix-map=/tmp/go-build3067507852=/tmp/go-build -gno-recor d-gcc-switches"

Use Golang on Debian 11

At this point, we will show you how to use Golang by creating your first project.

First, create a hello directory for your project with the command below:

sudo mkdir -p hello

Switch to your directory:

cd hello

Then, create a simple program to test your Golang installation on Debian 11. Create the file with your favorite text editor, here we use vi:

sudo vi hello.go

Add the following content into your file:

package main
import "fmt"
func main() {
fmt.Printf("Welcome To orcacore\n")
}

When you are done, save and close the file.

Now you need to build go.mod files so that you can execute the Golang file that you have created on Debian 11:

sudo vi go.mod

Add the following line to the file:

module example.com/mod

When you are done, save and close the file.

Build the program with the command below:

go build

Then, execute the program with the following command:

./mod

In your output you will see:

Output
Welcome To orcacore

Conclusion

At this point, you learn to install Golang on Debian 11. Also, you learn some basic usage of Go by creating a sample project.

Hope you enjoy it.

May you will be interested in these articles:

Install and Configure LibreNMS on Debian 11.

Install and Configure Django on Debian 11.

How To Install and Configure Mattermost on Debian 11.

How To Install and Configure Netdata on Debian 11.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!