Introduction: What Go Is
Go is a small, statically typed, compiled language for servers and tools, and what it leaves out is as deliberate as what it keeps.
Go is a programming language. You write instructions as plain text in a file. A program called the compiler reads that file and produces an executable: one file your computer can run on its own, with nothing else installed beside it.
Go is statically typed. A type is the kind of a value, such as a whole number or a piece of text. You write types down, and the compiler checks them before the program runs. Adding text to a number is caught when you build, not when a customer hits it.
What Go leaves out
The language has 25 keywords. There is no class inheritance, no exceptions, no operator overloading and no macros. An error is an ordinary value that a function hands back, so every place an error can appear is visible in the code. Code layout is decided by a tool, not by argument. Less to learn means more code you can read on your first day in an unfamiliar project.
What it is for
Go was built for servers and command line tools. Builds take seconds. The language has built-in support for doing several things at the same time, which Stage 5 covers. Docker, Kubernetes and Terraform are written in Go.
This course documents Go 1.27. Where behaviour depends on the version, the lesson says which version introduced it.
Example
package main
import "fmt"
func main() {
// A variable is a named place that holds a value.
// count holds whole numbers. The type for whole numbers is int.
var count int = 3
// A string is a piece of text.
var name string = "service"
fmt.Println(name, "started with", count, "workers")
// count = "three" would not build. int and string are different types,
// and the compiler checks that before the program ever runs.
}
// Output:
// service started with 3 workers
When to use it
- Decide whether a new HTTP service should be written in Go rather than a scripting language.
- Ship a command line tool as a single file a colleague can run with nothing installed.
- Read the source of infrastructure you already run, such as Docker or Kubernetes.
More examples
Types do not mix on their own
Go never guesses a conversion for you. If you want a number inside a piece of text you convert it yourself, here with strconv.Itoa. Reach for this whenever you are building a log line or a label out of numbers.
package main
import (
"fmt"
"strconv"
)
func main() {
count := 3 // a whole number
// count + " workers" does not compile: a number and text are different types.
// strconv.Itoa turns a whole number into text first.
line := strconv.Itoa(count) + " workers"
fmt.Println(line)
}
// Output:
// 3 workers
An error is a value you are handed back
This is the shape of almost every Go function that can fail: the result first, the error second. There is nothing to catch and nothing hidden, which is why error handling in Go is visible rather than invisible. Stage 3 covers errors properly.
package main
import (
"fmt"
"strconv"
)
func main() {
// strconv.Atoi turns text into a whole number.
// It hands back two things: the number, and an error value.
n, err := strconv.Atoi("41")
if err != nil {
// nil means "no error". This part runs only when the text was not a number.
fmt.Println("bad input:", err)
return
}
fmt.Println("next:", n+1)
}
// Output:
// next: 42
What the compiler says when types do not line up
The build stops and names the file, the line, the column and the problem. You will see messages in this shape constantly while learning, and they are the reason a Go program that builds has already had a whole class of mistakes removed from it.
$ go build .
./main.go:9:17: invalid operation: count + " workers" (mismatched types int and untyped string)
Discussion