Go Programming
Go from the first program to structs, methods, interfaces, errors, goroutines and channels, with every program compiled and run in your browser.
- Level
- Beginner
- Time
- About 20 hours
- Modules
- 5
- Lessons
- 15
- Exercises
- 45
Go is a small, fast and readable language made at Google. It powers cloud tools such as Docker and Kubernetes and many web servers, and it is loved for its simple syntax, quick builds and easy concurrency. This course teaches Go from the first line of code to structs, interfaces, error handling and goroutines.
Every example runs in your browser on a real Go compiler, and every exercise is checked against hidden tests. The programs are ordinary Go and also run unchanged on the Go tools on your own computer.
What you will learn
- Write, run and read Go programs that take input and print results
- Use types, control flow and functions with multiple return values
- Work with slices, maps, strings and runes
- Design programs with structs, methods, pointers and interfaces
- Handle problems with error values, panic and recover
- Run work at the same time with goroutines and channels
Skills you will gain
- Go syntax and types
- Slices, maps and strings
- Structs, methods and interfaces
- Error handling
- Goroutines and channels
Before you start
- No previous programming is required, though the Python or C course helps
- Comfort with basic arithmetic and reading careful instructions
- Nothing to install: Go runs in your browser
Where it is used
- Cloud and back-end services
- Command-line tools
- DevOps and infrastructure software
- Technical interviews and coding exercises in Go
Modules
Module 1: Go basics
The structure of a Go program, printing with fmt, variables, types and constants, and reading input.
- 1
Your first Go program
How a Go program is laid out, what package main and func main mean, how go build turns source into a program, and how to print with fmt.Println, fmt.Print and fmt.Printf.
- 2
Variables, types and constants
Declaring variables with var and :=, the basic types int, float64, string, bool, byte and rune, zero values, converting between types, and constants.
- 3
Reading input
Reading numbers and words with fmt.Scan, reading whole lines with bufio.Scanner, splitting lines with strings.Fields and converting text to numbers with strconv.Atoi.
Module 2: Control flow and functions
Decisions with if and switch, loops with for, and functions that return several values.
- 1
Conditions and switch
Making decisions with if, else if and else, comparison and logical operators, the if statement with a short setup, and switch with several values per case or no value at all.
- 2
Loops with for
Go's only loop keyword, for, in its three forms: with a counter, with just a condition, and forever; plus break, continue, nested loops and range over a string.
- 3
Functions and multiple return values
Writing functions with parameters and results, returning several values, named results, variadic parameters, functions as values, closures, recursion and defer.
Module 3: Slices, maps and strings
Lists with arrays and slices, key and value pairs with maps, and text with strings and runes.
- 1
Arrays and slices
Fixed-size arrays, growable slices, append, len, make, slicing with a[low:high], range, copy, sorting with sort.Ints and slices of slices.
- 2
Maps
Storing key and value pairs with maps: creating, adding, reading with the comma ok form, deleting, counting, maps of slices, and printing in a fixed order by sorting the keys.
- 3
Strings, runes and the strings package
How Go stores text as bytes, the difference between bytes and runes, indexing and slicing strings, looping with range, and the useful functions of the strings package.
Module 4: Structs, methods and interfaces
Your own types with structs, behaviour with methods and pointers, and flexible code with interfaces.
- 1
Structs
Grouping related values into your own type with struct: declaring, creating with named fields, reading and changing fields, nesting, slices of structs, comparing and printing structs.
- 2
Methods and pointers Intermediate
Adding methods to your types, pointers with & and *, value and pointer receivers, why a method must use a pointer to change its value, constructors and the String method.
- 3
Interfaces Intermediate
Describing behaviour with interfaces: implicit implementation, using an interface as a parameter or slice element, type assertions and type switches, the empty interface, and sort.Interface.
Module 5: Errors, goroutines and a complete program
Handling problems with errors, running work at the same time with goroutines and channels, and building a small command-line program.
- 1
Errors, panic and recover Intermediate
Go's way of handling problems: error values returned from functions, errors.New and fmt.Errorf, wrapping with %w, errors.Is and errors.As, custom error types, and panic with recover.
- 2
Goroutines and channels Intermediate
Running work at the same time with goroutines, passing values with channels, buffered channels, closing and ranging over a channel, waiting with sync.WaitGroup, protecting data with sync.Mutex and choosing with select.
- 3
Putting it together: a command-line program Intermediate
Designing and writing a small complete Go program from commands read on the input, using structs, methods, maps, slices, errors and a command loop.
Projects
- A command-line task manager Build a program that keeps a list of tasks and obeys commands from the input: add a task, mark it finished, list the tasks, show counts and remove one. Every mistake in a command is reported clearly and the program carries on.
- A text analyzer Build a program that reads a text from the input and reports statistics: the number of lines, words and characters, the most common words, the longest word and how often each letter appears.
- A concurrent job runner Build a program that reads a list of jobs, runs them with a pool of worker goroutines, collects the results through channels and prints a report in the order of the jobs, including the jobs that failed.
What to learn next
- Data Structures Lists, stacks, queues, trees, heaps, hash tables and graphs, built in C node by node, with every pointer drawn.
- Algorithms Searching, sorting, recursion, greedy choices, dynamic programming and graph algorithms, with the cost of each one counted.
- Object-Oriented Programming in C++ Classes, constructors and destructors, operators, inheritance, virtual functions, templates, the STL and exceptions, with every object on screen.