Build robust Go programs through idiomatic error handling: error interface, Sentinel Errors, wrapping with %w, Custom Error Types, panic/recover, and HTTP mapping.
Go (often called "Golang") was created at Google starting in 2007 by Robert Griesemer, Rob Pike and Ken Thompson — as the answer to the problems of large C++ codebases: slow builds, complex concurrency, awkward tooling.
The result is a simple language for complex systems:
go func() { ... }() starts a goroutine;
communication via channels instead of shared-state threading.gofmt enforces one format, go test is part of the
language, go vet catches real mistakes, go mod manages dependencies.// The most famous short Go program — showing the essence:
// a main package, a main function, no semicolon fuss, no classes.
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
// → Hello, Go!
}
Packages are the unit
In Go everything is a package. Every file starts with package <name>.
An executable program begins in the main package with func main(). Code
from other packages (like fmt) is brought in with import — imports you
don't use are a compile error.
⚠ Common mistake: The same holds for variables:
x := 5infunc main()that is never read breaks the build withdeclared and not used. While experimenting, use the value, deliberately discard it with_ = x— or delete the line and the import again.
Check: What does an executable Go program start with?
Answer: package main and a function main()
A Go program always starts with the main package and func main(). Without those two it becomes a library, not an executable.
go mod init <module> creates a new Go module (writes go.mod).
go run . compiles and runs your program. go build produces the
binary.
# Create the project
mkdir hello && cd hello
go mod init hello
# write hello.go, then run it
go run .
# Hello, Go!
# and build a binary (hello on Linux/Mac, hello.exe on Windows)
go build -o hello
// hello.go
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}
go.mod — the module manifest
go.mod is the one file that defines every Go project. It names the module
(e.g. hello or your real domain path like github.com/name/project) and
lists the Go version and dependencies.
module hello
go 1.24
Go does not need semicolons, but the compiler inserts them automatically at the end of a line. That is why this classic mistake does NOT work — the opening brace belongs at the end of the line:
// This does NOT work — the { belongs with the signature.
func main()
{
}
// Correct — short but that is the whole rule:
func main() {
}
Check: Which command creates the go.mod manifest and starts a new Go module?
Answer: go mod init hello
go mod init <name> creates go.mod, which declares the module and its dependencies. Anything else is fiction.
Go includes its quality tooling as part of the language — nothing extra to install:
| Command | What it does |
|---|---|
gofmt -w file.go | Formats your code to the official standard |
go vet ./... | Finds suspicious constructs (beyond syntax) |
go test ./... | Runs tests — testing is built in |
go doc <package> | Shows a package's documentation in the terminal |
go run . | Runs the current package |
go build ./... | Compiles everything without running it |
gofmt ends all flame wars
In Go there is one official formatting, enforceable with gofmt (or
go fmt ./... in a project). Tabs or spaces, brace on a new line — only one
answer is right. That way every team member reads every file the same way.
package main
import "fmt"
func main() {
weight := 12
height := 1.70
// vet warns: mismatched types int and float64 — that is NOT intended!
index := weight / height
fmt.Println(index)
}
go vet reports the type mismatch here. Correct version: convert both values
explicitly to float64 — more on that in Module 2.
Check: Which command brings your code to the official Go standard?
Answer: gofmt
gofmt (or go fmt ./... in a project) formats to the one official Go style. There is no room for alternatives.
Go's standard library is big and well thought out — most tasks you solve without ever installing a package from the network.
package main
import (
"fmt"
"os"
"sort"
"strings"
)
func main() {
// fmt.Sprintf: assembling text
msg := fmt.Sprintf("User: %s, attempts: %d", "ana", 3)
// strings: everything around text
upper := strings.ToUpper("hello")
parts := strings.Split("a,b,c", ",") // ["a" "b" "c"]
// sort: sort in place
nums := []int{3, 1, 2}
sort.Ints(nums) // [1 2 3]
// os: arguments and environment
args := os.Args[1:]
fmt.Println(msg, upper, parts, nums, args)
// → User: ana, attempts: 3 HELLO [a b c] [1 2 3] []
}
⚠ Common mistake:
fmt.Println("User: %s", name)interpolates nothing —Printlnhas no format verbs and literally printsUser: %s ana. Formatting is the job offmt.Printf(ending with\n) orfmt.Sprintfas in the example above.
No classes, but methods
Strings, slices, maps — all are values with associated functions.
strings.ToUpper is a function of the library, not a method on a String
class. Still, you can give your own types methods (Module 2, lesson 10).
# A function's signature
go doc strings.Split
# The full package docs
go doc strings | head -20
Check: Which package contains text functions like Split and ToUpper?
Answer: strings
All the handy text helpers live in strings: split, case conversion, replace, trim — no extra package needed.
In Go you write tests with the built-in testing package. A file
file_test.go next to the code, a function TestXxx(t *testing.T), and
go test just runs — no framework, no mocking library, no configuration.
// add.go
package main
func add(a, b int) int {
return a + b
}
// add_test.go
package main
import "testing"
func TestAdd(t *testing.T) {
if got := add(2, 3); got != 5 {
t.Errorf("add(2, 3) = %d, want 5", got)
}
}
go test ./...
# ok hello 0.002s
Table-driven tests — the Go pattern
Instead of writing many test functions, Go favors table-driven tests: a table of inputs and expected outputs, one loop, one test.
func TestAddTable(t *testing.T) {
cases := []struct {
a, b, want int
}{
{2, 3, 5},
{0, 0, 0},
{-1, 1, 0},
}
for _, c := range cases {
if got := add(c.a, c.b); got != c.want {
t.Errorf("add(%d, %d) = %d, want %d", c.a, c.b, got, c.want)
}
}
}
Take 3 minutes: answer the four questions from this module without help. Answered correctly? Move on to Module 2.
Check: What is the difference between go run and go build?
Answer: go run compiles and executes; go build only produces the binary
go run . compiles in memory and starts immediately. go build produces the executable binary you can distribute anywhere.
Course navigation: All 20 modules · Module 2 →