Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Set-up · 20 minutes

Set up your Go lab

After this lesson you can
  • Install Go 1.22 or later on your own computer
  • Create the lab folder ~/krlab/go
  • Prove with a script that the lab works
Lab files of this lesson (1)

The lab folder as it stands after this lesson. Unzip it in ~/krlab.

More

The zip holds every file that the course has printed up to here. Type the code yourself first: you learn more.

What you need

  • A computer that you own, with Linux, macOS or Windows. The lessons show Linux commands. On Windows, use WSL (Windows Subsystem for Linux), which gives you a Linux shell.
  • About 500 MB of free disk space and a terminal.
  • Go 1.22 or later. The lessons use for range over a number and routes such as GET /health, and both need 1.22.
  • A C compiler (gcc or clang). The race detector of lessons 6 to 8 needs one. On Debian or Ubuntu, run sudo apt install gcc. On macOS, run xcode-select --install.
  • No account, no paid tool and no graphics card.

Everything in this course runs on your own machine, in the folder ~/krlab/go. The programs of the course open ports only on 127.0.0.1, the address that answers your own machine and nobody else.

Set it up

1. Check if you have Go

go version prints the version of Go, or an error if Go is not installed.

go version
go version go1.27.3 linux/amd64
The line that go version prints
go versionthe words that the command always printsgo1.27.3your Go release: 1.22 or later is neededlinux/amd64your system and CPU typego versionthe words that the command always printsgo1.27.3your Go release: 1.22 or later is neededlinux/amd64your system and CPU type

If the version is 1.22 or later, go to step 3. If you get command not found, or the version is older, install Go in step 2.

2. Install Go

The Go project publishes Go as a download for each system. This is the one place where the course names an outside site: the official download page of the Go project, go.dev/dl. Pick one of these ways. This course cannot test an install on your machine, so read the result of step 1 again after each one. The download page shows a SHA-256 checksum for each file. After the download, run sha256sum go*.tar.gz and compare the value with the page. A different value means: do not use the file.

  • A package from the Go project, for any system. On the download page, take the archive for your system and CPU. On Linux you unpack the .tar.gz file into a folder, and then you add the folder bin of that folder to your PATH. With /usr/local/go this needs administrator rights. Without them, unpack the archive into ~/.local (so you get ~/.local/go) and add ~/.local/go/bin to the PATH in the file ~/.profile. On macOS and Windows the page offers an installer.
  • A package manager. On many Linux systems sudo apt install golang-go works. The version may be older than 1.22: check it. On macOS, brew install go works if you use Homebrew.

PATH is the list of folders in which the shell looks for programs. After you change it, open a new terminal and run go version again.

3. Make the lab folder

mkdir -p makes a directory and the directories above it. cd moves you into a directory.

mkdir -p ~/krlab/go
cd ~/krlab/go
pwd
/home/you/krlab/go

4. Write the check script

The script labcheck runs four tests. It prints one line for each test and a last line with the count. Save it as labcheck, then make it executable with chmod +x labcheck (change mode, add the right to execute).

labcheck

#!/usr/bin/env bash
# labcheck: checks the Go lab of the KaliRange course "Go for security tools".
ready=0

# 1. go runs
if go version >/dev/null 2>&1; then
  echo "1 ok    go runs"
  ready=$((ready + 1))
else
  echo "1 FAIL  go does not run (is it on your PATH?)"
fi

# 2. the version is 1.22 or later
ver=$(go env GOVERSION 2>/dev/null | sed 's/^go//')
major=${ver%%.*}
rest=${ver#*.}
minor=${rest%%[!0-9]*}
case $major in ''|*[!0-9]*) minor="" ;; esac
if [ -n "$minor" ] && { [ "$major" -gt 1 ] || [ "$minor" -ge 22 ]; }; then
  echo "2 ok    go is 1.22 or later"
  ready=$((ready + 1))
else
  echo "2 FAIL  go is older than 1.22 or its version is unknown"
fi

# 3. a tiny program builds and runs
dir=$(mktemp -d)
cat > "$dir/main.go" <<'GO'
package main

import "fmt"

func main() { fmt.Println("lab-ok") }
GO
printf 'module labtest\n\ngo 1.22\n' > "$dir/go.mod"
if [ "$(cd "$dir" && go run . 2>/dev/null)" = "lab-ok" ]; then
  echo "3 ok    a program builds and runs"
  ready=$((ready + 1))
else
  echo "3 FAIL  a program did not build and run"
fi

# 4. the race detector works (it needs a C compiler)
if [ "$(cd "$dir" && go run -race . 2>/dev/null)" = "lab-ok" ]; then
  echo "4 ok    the race detector works"
  ready=$((ready + 1))
else
  echo "4 FAIL  the race detector does not work (is a C compiler installed?)"
fi
rm -rf "$dir"

echo "lab ready: $ready of 4"
[ "$ready" -eq 4 ]
chmod +x labcheck

The script makes a temporary folder with mktemp -d, builds a small program in it, runs it with and without the race detector, and deletes the folder. It needs no network.

Check your lab

Run the script. ./ starts a program in the current folder. The first run can take 30 seconds or more, because Go builds its race detector once. Later runs are fast.

./labcheck
1 ok    go runs
2 ok    go is 1.22 or later
3 ok    a program builds and runs
4 ok    the race detector works
lab ready: 4 of 4

Your lab is ready when all four lines say ok. Type the last line that your script printed.

If it fails

What you seeWhat it meansWhat to do
1 FAIL and command not foundGo is not installed, or the shell cannot find it.Install Go (step 2). Then check that the folder bin of Go is in your PATH, and open a new terminal.
2 FAILThe version is older than 1.22.Install a newer Go. An old golang-go package from a package manager is the usual cause. Run which go to see which one the shell uses.
3 FAILGo starts, but a program does not build.Run go version and go env GOROOT, and check that the folder exists. Check that your disk is not full and that your home folder is writable.
4 FAILThe race detector needs cgo (the part of Go that calls C code), and cgo needs a C compiler. Go says -race requires cgo when it has none.Install gcc (see "What you need"), then run go env CGO_ENABLED. It must print 1.
Permission denied when you run ./labcheckThe script has no right to execute.Run chmod +x labcheck.
go: downloading go1.xxYour Go starts a newer toolchain that it must download.The lab needs no network, so run go env -w GOTOOLCHAIN=local and try again.

The lab zip of the course page holds the files that the lessons print. After you unzip it, go into the folder go that it makes. There, sh kr-lab.sh 09-build-a-port-checker ~/krlab/go builds the lab folder as it stands after lesson 9. Each program has its own folder and its own go.mod (hello, funcs, portcheck and so on), so no file replaces another, and you build one program at a time: cd ~/krlab/go/portcheck and go test ./.... The name above a code block is its path from ~/krlab/go. The zip of lesson 10 holds only the test server and the input file, because you write that tool yourself.

When the lab is ready, go on to lesson 1.