Preface
This book teaches Go to someone who already programs. If you have written loops and functions in Lux, Python, JavaScript, or anything of that family, you have what you need. You will not be told what a variable is. You will be told what Go does with one, a shorter conversation than in most languages and a more useful one than you expect.
Go is small, and it stays small deliberately. There is one loop keyword, one way to format source, no classes, no exceptions, and no inheritance. A language that size can be held in your head entire, and that is the thing Go is buying: you spend your attention on the problem rather than on the language, and a program someone else wrote reads like one you wrote. The cost is that Go declines to do some work for you. A function that can fail hands the failure back as an ordinary value and you deal with it on the next line. A value gets copied when you pass it, unless you ask for a pointer. Nothing checks that you covered every case. Coming from a scripting language you will notice the typing first, but it is this second habit — the language handing jobs back rather than absorbing them — that takes the longest to settle into, and Chapters 5 and 8 are where it gets its due.
What you get in return arrives early. The compiler is fast enough that building
is not a pause in your work. gofmt ends every argument about layout by
reformatting your source to the one true shape. The standard library is broad
enough that most programs need no dependencies at all, and the tool that
builds, tests, formats, and documents your code is the same go command with a
different first word. Then there is concurrency, Go’s headline and Chapter 11:
a goroutine costs a keyword and a few kilobytes, so structuring a program
around dozens of concurrent pieces is an ordinary decision rather than an
architectural one.
The pace is brisk on purpose. Programs come first and explanation follows, and the explanation stops when the point has been made. A feature you can already guess the shape of gets a sentence. A feature that will bite you gets a page. When you want the complete description of some corner of the language, the Go Programming Language Specification is short, readable, and free, and this book is not trying to replace it.
Read it with a keyboard. Type the programs in, run them, then break them and read what the compiler says. The exercises are short and most are variations on the program just presented; do the ones that look interesting and skip the rest without guilt.
Getting the compiler
Install Go from https://go.dev/dl. That gives you the go command: the
compiler, the test runner, the formatter, the package manager, and the
documentation browser. Check it with go version.
The code here needs Go 1.23 or newer, released in August 2024. The newest things
it uses are ranging over an integer and the iterator forms in the slices and
maps packages. Where a program’s output depends on your machine, the text says
so.
About the examples
Every complete program in this book lives in the examples directory beside the
text, one directory per program. From examples, running
go run ./ch01/fahr2
builds and runs the Fahrenheit table from Chapter 1. The programs are the ones printed here, not adaptations, so if something does not behave the way the text claims, the text is wrong and you should trust the compiler.
The programs that deliberately fail to compile live in examples/errors, one
directory per mistake. Every compiler message quoted in this book came from
running go build on the file of that name, and Appendix C collects them.
A note if you are coming from Lux
Lux and Go are close in the places that matter for getting started and far apart
in two specific spots. Close: types are written after the name, functions are
func, a for loop over a range reads almost the same, and structs are
structs.
The two that are far apart are the ones Lux was careful about. Lux has no null;
Go has nil, and reaching for it is normal rather than a smell. Lux has enums
with associated values and a match that must cover every case; Go has neither,
and Chapter 9 shows what you build instead. Neither of those is Go being
careless. They are a different bet about where safety comes from — the compiler
in one language, the programmer and the tooling in the other — and the way to
learn Go is to make the bet on purpose rather than to keep reaching for the
guarantee that is not there. lux convert go program.lux prints the Go
translation of any Lux program you have written, and reading a few of those
against the originals is the fastest map between the two. Appendix B has the
construct-by-construct table.