Learn What is Go?

Go is a compiled language from Google. That means a program called the compiler checks all of the code first, then makes one binary file (a program file of…

Many companies use Docker, Kubernetes and Terraform, three tools for managing servers. They have one thing in common: all three are written in Go. The language was made at Google and announced in 2009. The first thing you will notice: Go is compiled. That means that before your program runs, a program called the compiler checks all the code in your .go file, then turns it into one program file that is ready to run.

Compiling works like the central kitchen of Bimo's Meatball Stall. The central kitchen first checks a recipe line by line. If the recipe lists an ingredient that was bought but never used, the kitchen refuses the recipe. If the recipe passes, the kitchen packs frozen meatballs into one pack. Any branch only has to heat that pack, with no kitchen and no recipe needed. In Go, the recipe is the .go file, the central kitchen is the compiler, the pack is the program file (called the binary), and a branch is a computer that runs the program.

Your first Go program

Throughout this Go track we build one program that keeps growing: the till program of Bimo's Meatball Stall. The menu is Tendon Meatballs at $15, Chicken Noodles at $12 and Iced Tea at $5. Every price is stored as a whole number of dollars. This is the first version:

package main

import "fmt"

func main() {
	fmt.Println("Bimo's Meatball Stall")
	fmt.Println("Tendon Meatballs", 15)
	fmt.Println(15 * 3) // 3 portions
}

Save it as stall.go, then run it in the terminal (the window where you type commands). You don't type the $ sign at the start of the line:

$ go run stall.go
Bimo's Meatball Stall
Tendon Meatballs 15
45

To try it on your own computer, install Go from go.dev/dl, then check it with go version. The result looks like go version go1.27.4 linux/amd64 (the version number and the system name may differ). In the BelajarWeb.dev exercises, the Run button uses a Go simulator in the browser, so you don't need to install anything yet.

Compiled: checked first, then one program file

There are two commands to run Go code:

This is go build in the terminal. The ls command lists what is in the folder:

$ go build stall.go
$ ls
stall  stall.go
$ ./stall
Bimo's Meatball Stall
Tendon Meatballs 15
45

A go build that works prints nothing. The stall file is a binary: a file of machine code, which is instructions the processor runs directly. It is about 2 MB in size and has everything it needs inside. So a server that runs stall does not need Go installed. Python and JavaScript are different: they need an interpreter (a program that reads the code and runs it) on the target computer.

The Go runtime goes inside that binary too: support code that works while your program is running. One part of it is the garbage collector, which automatically removes data in memory that is no longer used. So you never free memory yourself. Go is also statically typed: the type of every value (a number, a text, and so on) is checked at compile time, not while the program is already running.

A strict compiler

The Go compiler refuses code that has an unused variable or import. In the version of stall.go below, the last line inside main stores the result of 15 * 3 in the variable total (the := sign makes a new variable). But total is never used. Before you look at the output, guess: do the two Println lines above it still print, or does no line run at all?

package main

import "fmt"

func main() {
	fmt.Println("Bimo's Meatball Stall")
	fmt.Println("Tendon Meatballs", 15)
	total := 15 * 3 // 3 portions
}

This is the compiler's answer:

$ go run stall.go
# command-line-arguments
./stall.go:8:2: declared and not used: total

An unused import is refused too. If you add "os" to the imports without using it, the result is ./stall.go:5:2: "os" imported and not used. Remove that import, or use it. If there is no func main, for example because you named it func start, the error is function main is undeclared in the main package.

Goroutines: many jobs at once

One big reason people like Go for the backend (programs on the server side) is the goroutine: a function that runs at the same time as other functions. To make one, you only write go in front of a function call. Goroutines are managed by the Go runtime, not by the operating system. One goroutine starts with about 2 KB of stack (the memory for its function calls). For comparison, a thread (a line of work managed by the operating system) usually needs megabytes of memory. Because goroutines are light, one Go server can run thousands of them at once, for example one goroutine for each incoming order. The details come in the goroutine lesson later.

🎯 What Go is used for

Go is simple on purpose: it has only 25 keywords (words the language reserves, like func and package). Go is not a good fit for frontend views in the browser or native mobile apps.

⚠️ Errors you will almost surely meet in your first week

TL;DR: Go is a compiled language from Google. That means a program called the compiler checks all of the code first, then makes one binary file (a program file of machine code) that runs on a computer without Go installed. go run stall.go compiles and runs right away, while go build stall.go makes a stall file you run with ./stall. A program must have package main, an import for each package (group of code) it uses, and func main() as its starting point. fmt.Println prints one line. An unused variable or import makes the compile fail (declared and not used, imported and not used), and when the compile fails, not a single line runs. Go has a garbage collector (it automatically removes data that is no longer used) and is statically typed (the types of values are checked at compile time). A goroutine, a function that runs at the same time as other functions, is very light (its memory starts at about 2 KB), so thousands can run at once.

⚡ Your first Go program

Write:

package main

import "fmt"

func main() {
	fmt.Println("Bimo's Meatball Stall")
	fmt.Println("Tendon Meatballs", 15)
	fmt.Println(15 * 3) // 3 portions
}

Meaning: package main marks a program you can run. import "fmt" uses fmt, a package (group of code) that comes with Go, for printing. Go runs what is inside func main() from top to bottom. Each fmt.Println prints one line to the terminal (the window where you type commands). Text goes inside double quotes, and 15 * 3 is calculated first, so 45 is printed.

⚡ A strict compiler

Guess first: Go checks all of the code before it runs it. In the program from the step before, the last Println line becomes total := 15 * 3 (:= makes a new variable), and the variable total is never used. Do the two Println lines above it still print?

No. Before the program runs, the compiler (the program that checks all the Go code) finds the unused variable total. The compile fails with the message declared and not used: total, so not a single line runs.

Meaning: Checking all of the code before the program runs is called compiling. An unused variable or import makes the compile fail. Remove it, or use it.

⚡ One file, ready to run

Write:

$ go build stall.go
$ ./stall

Meaning: Go is a compiled language: the code is first turned into a program file, not read line by line by another program (an interpreter) while it runs. go build makes one binary file (a program file of machine code), here named stall, and ./stall runs it. A server that runs that file doesn't need Go installed. While you are still experimenting, go run stall.go compiles and runs right away. You don't type the $ sign at the start of a line.

What you will learn