Skip to content

Go Basics

Glossary

137 terms from Go Basics, in plain words, each with the chapter that teaches it.

#

&& , || , ! (logical operators)
`&&` means "and" (both conditions true), `||` means "or" (at least one true), and `!` flips true to false and back. Alex's shipping rule uses them, e.g. `wholesale && bags >= 10`.
:= (short variable declaration)
Creates a new variable and gives it a value, with Go working out the type from that value. Use plain `=` to change a variable that already exists.

A

Address
The place in the computer's memory where a value is stored. `&mug` gives the address of the `mug` product.
Anonymous struct
A struct type written in place without a `type` name, used when it's needed only once. Table tests use it for their list of cases.
any
The interface with no methods, so every type satisfies it. A variable of type `any` can hold any value, which is how `fmt.Println` accepts whatever you pass it.
append
Built-in function that returns a slice with new elements added at the end. Always store the result back, as in `orders = append(orders, 5500)`.
Argument
A value you pass to a function when you call it. In `dollars(3405)`, `3405` is the argument.
Array
A fixed-length row of values in memory. A slice is a small view onto a hidden array.
Atoi
A `strconv` function that turns text into an `int`, returning the number and an error. Priya's order form uses it to check quantities typed as text.

B

Blank identifier
The underscore `_`, a place to throw away a value you don't need, such as an unused result or the index in a `range` loop.
bool
A type that holds either `true` or `false`. Every comparison gives a `bool`.
break
A keyword that leaves a loop immediately, skipping any remaining passes. For example, it can stop a loop over the days of the week once Friday is reached.
Breakpoint
A line, shown as a red dot in VS Code, where the debugger pauses your program so you can inspect it.
Byte
The small unit text and files are stored in. English letters take one byte, but accented letters such as é take two, and `[]byte` is a slice holding a file's raw contents.

C

Call
To run a function by writing its name and arguments, such as `fmt.Println("Hi")`.
camelCase
Go's habit of joining words with capitals instead of spaces, such as `orderTotal`. Start such names with a lowercase letter, because a leading capital makes a name exported.
case and default
Parts of a `switch`: each `case` lists values to match, and `default` runs when nothing matches.
Cents (money as whole numbers)
A way of storing money as a whole number of cents in an `int`, because `float64` can't store most decimal fractions exactly. The amount is converted to dollars only when printing, using `/ 100` for the dollars and `% 100` for the cents.
Channel
A Go type that lets goroutines pass values to each other safely. One goroutine sends a value into the channel and another receives it.
Command-line arguments
The words typed after a program's name, stored as a slice of strings in `os.Args`. Index 0 is the program itself, so your arguments start at index 1.
Comment
A note for people, written after `//`, that Go ignores.
Comparison operators
Operators such as `==`, `!=`, `<` and `>=` that compare two values and give a `bool`. A single `=` stores a value, while `==` compares.
Compiled language
A language whose source code is translated into an executable before it runs. Go is one.
Compiler
The program that translates your source code into an executable. It stops with an error message if it can't understand your code.
Constant
A value that never changes, named with `const`, such as `bagsPerCase`. Go stops you from changing it by accident.
Constructor
By convention a function named `New` plus the type name, such as `NewProduct`, that makes and checks a new value. It usually returns a pointer, or `nil` and an error.
continue
Keyword that skips the rest of the current pass of a loop and moves on to the next one.
Control flow
How a program chooses which lines run and how often, using `if`, `switch` and `for`.
Coverage
A measure of how much of your code the tests ran. `go test -cover` prints a percentage, and a coverage profile can become an HTML page showing missed lines in red.
Cross-compiling
Building a program for a different operating system or processor than your own, by setting `GOOS` and `GOARCH` before `go build`.
CSV (comma-separated values)
A table stored as plain text, one row per line with commas between values. Kettle & Crane's `stock.csv` is one, and spreadsheets can open it.

D

Debugger
A tool that pauses a running program so you can look at its variables and step through it line by line. VS Code's Go extension has one.
Declare
To create a variable, constant, function or type by naming it.
defer
Schedules a call to run just before the current function returns, however it returns. `defer f.Close()` right after opening a file makes sure the file is closed.
delete
Built-in function that removes a key from a map, as in `delete(stock, "SO-5LB")`.
Doc comment
A comment directly above an exported name, by convention starting with that name. `go doc` displays it.
Duration
A length of time, such as the gap between two times returned by `Sub`. `Hours()` converts it to hours.

E

Early return
Returning from a function as soon as something fails, so the main path stays easy to follow.
Element
One item in a slice or array, found by its index.
encoding/csv
The standard-library package for reading and writing CSV files. `ReadAll` returns all rows as a `[][]string`, and `WriteAll` writes one back.
ErrNotFound
A fixed error value, made once with `errors.New`, that callers can recognize with `errors.Is`. By convention such names start with `Err`; the inventory tool uses it for unknown SKUs.
Error
A value of the built-in `error` type that describes what went wrong, or `nil` if nothing did. A function that can fail returns one as its last result, and you check it with `if err != nil`.
errors.Is and errors.As
`errors.Is` asks whether an error is (or wraps) a particular error value, such as `ErrNotFound`. `errors.As` asks whether it is of a particular type and, if so, fills a variable so you can read its fields.
errors.New and fmt.Errorf
Ways to make your own errors: `errors.New` makes one with a fixed message, and `fmt.Errorf` builds one with `Printf` verbs and can wrap another error with `%w`.
Escape
A backslash code inside a string, such as `\n` for a new line, `\t` for a tab or `\"` for a quote mark.
Executable (binary)
A single file of machine instructions that you can run like any other app. `go build` makes one, and it runs even on computers without Go.
Exit code
A number a program ends with, telling the terminal whether it succeeded: 0 for success and anything else for failure. `os.Exit(1)` ends the program with code 1.
Exported
A name that starts with a capital letter, so code in other packages can use it, such as `catalog.Find`.
Extension
An add-on that teaches VS Code about a language. The Go extension adds formatting, error underlining and a debugger.

F

Field
A named value inside a struct, such as `Stock` in `Product`. You read it with a dot: `mug.Stock`.
float64
A type for numbers with a decimal point. It can't store most decimal fractions exactly, so it isn't used for money.
fmt
The standard-library package, short for "format", that prints text, e.g. `fmt.Println` and `fmt.Printf`.
for
Go's only loop keyword. It can count, repeat while a condition holds, run forever until `break`, or use `range`.
Function
A named list of instructions that can take parameters and return results, so you can run a job without repeating the code. `main` is the function Go runs first.

G

go build
Command that compiles the module into an executable named after it, saved in the folder.
go doc
Command that shows documentation in the terminal for standard-library or your own packages, e.g. `go doc strings.TrimSpace`.
go fmt
Command that rewrites your files in Go's one standard layout and prints the names of the files it changed. The `gofmt` tool does the work.
go get
Command that downloads someone else's module and adds it to `go.mod`, e.g. `go get github.com/google/uuid`.
go install
Builds a program like `go build` but puts the executable in the `bin` folder of Go's workspace. With that folder on your PATH, you can run your tool by name from anywhere.
go mod init
Command that starts a new module in the current folder by writing `go.mod`. Run it once in each new project folder.
go mod tidy
Command that adds missing requirements to `go.mod`, removes unused ones and updates `go.sum` to match your imports.
Go Playground
A website at go.dev/play that runs small Go programs in a browser, handy on a computer without Go installed.
go run
Command that compiles your code and runs the result straight away. The dot means "the module in this folder".
go test
Command that runs your tests and reports PASS or FAIL. `-v` lists each test, `-run` picks tests by name, `-cover` measures coverage, and `./...` means this folder and everything below.
go vet
Command that looks for code that compiles but is probably wrong, such as a `Printf` with a missing value.
go.mod
A small file at the root of a module that records the module's name, the Go version and any required modules.
go.sum
A file of fingerprints for the modules a project downloaded, so nobody can swap the code unnoticed.
GOOS and GOARCH
Settings for the operating system and processor family a build targets, e.g. `windows/amd64`. Set them before `go build` to cross-compile.
Goroutine
A function that runs at the same time as other functions in the same program. Goroutines let a Go program do several things concurrently.

H

Header
The first row of a CSV file, naming the columns. The stock tool skips it with `rows[1:]`.
HTTP server
A program that answers web requests; the next book builds one with `net/http` to turn the inventory into a web service.

I

if and else
`if` runs a block only when its condition is `true`, `else if` tests further conditions, and `else` covers the rest. Go runs the first block that matches.
import
Keyword that brings another package into a file, such as `import "fmt"`. Go refuses unused imports.
Import path
The name used to import a package: the module name plus the folder, such as `"kettle/shop/catalog"`.
Index
A position in a slice or string, counting from 0. `coffees[0]` is the first element.
int
The type for whole numbers, which can be negative. Dividing two `int` values drops the fraction.
Interface
A type that lists methods. Any type that has all of them satisfies it automatically, so one function can work with many types, like `quote` with every `Shipper`.
Itoa
The `strconv` function that turns an `int` into text, the partner of `Atoi`.

K

Keyword
A word with a fixed meaning in Go, such as `func` or `for`, which you can't use as a name.

L

len
Built-in function that gives the number of elements in a slice or map, or the number of bytes in a string. The last index of a slice is `len(s)-1`.
Literal
A value typed straight into your code, such as `42` or `"mug"`. Go treats it as a constant.

M

main function
The function called `main` in `package main`, which Go runs when the program starts. A program without one fails with `function main is undeclared in the main package`.
Map
A collection of key-value pairs where you look up a value by its key, like SKU to stock count. Iteration order is random, and `v, ok := m[k]` tells you whether a key exists.
Method
A function attached to a type through a receiver, called with a dot, like `mug.Value()`.
Module
A whole project: a folder with a `go.mod` file, plus the packages in it and its subfolders. Other people's modules can be added with `go get`.

N

nil
Go's word for "no value": for errors it means no error, and it is also the zero value of pointers, slices and maps.
nil map
A map that was never created, as with `var counts map[string]int`. You can read it, but adding a key panics.
nil pointer dereference
A panic caused by reading a field through a pointer that is `nil` and so points nowhere. It is one of the most common Go crashes.
nil slice
An empty slice, the zero value of a slice type. It's ready to use with `append`.

O

Operating system
The software that runs a computer: Windows, macOS or Linux. A compiled program runs only on the operating system and processor type it was built for.
os.Args and os.Exit
`os.Args` holds the command-line arguments as a slice of strings, and `os.Exit(code)` ends the program immediately with an exit code.
os.Open and os.Create
`os.Open` opens a file for reading, and `os.Create` opens one for writing, creating it or emptying it first. Both return a file and an error, and the file must be closed afterwards.
os.ReadFile and os.WriteFile
Functions that read or write a whole file at once, working with `[]byte`.

P

Package
A folder of Go files that share a `package` line and can use each other's top-level names. The package called `main` becomes a runnable program.
Package-level variable
A variable declared outside any function, so every function in the package can use it, like `products` in `catalog`.
Panic
A crash caused by a bug the compiler couldn't catch, such as an index out of range or dividing by zero. Treat a panic as a bug to fix, not as an error value.
Parameter
A variable in a function's definition that receives an argument when the function is called. In `dollars(cents int)`, `cents` is the parameter.
PATH
The list of folders where the terminal looks for commands. Go's installer adds Go's `bin` folder to it so `go` works from anywhere.
Permissions
Settings on macOS and Linux for who may read or change a file. In `0644`, the leading 0 marks a base-8 number, 6 lets you read and write, and each 4 lets others only read.
Pointer
A value holding the address of another value. `&mug` gives a pointer to `mug`, and its type is written `*Product`, letting a function or method change the original.
Pointer receiver
A receiver written `(p *T)` so a method can change the original value instead of a copy. Use one when the method changes the value.
Println and Printf
`fmt.Println` prints values followed by a new line, and `fmt.Printf` slots values into verbs and doesn't end the line itself. `Sprintf` works like `Printf` but returns the string instead of printing it.

R

range
Used with `for` to loop over every element of a slice or map, giving the index (or key) and the element (or value). With `range 3` it counts from 0.
Receiver
The value a method is called on, written in parentheses before the method name, like `(p Product)`.
Remainder
What is left over after whole-number division, given by the `%` operator. `7 % 2` is 1.
return
Keyword that ends a function and hands its result back to the line that called it.

S

Satisfy
A type satisfies an interface when it has every method the interface lists. Go does this automatically, without the type naming the interface.
Scope
The part of the code where a name can be used. A variable declared inside braces exists only until the closing brace.
Shortcut operators (+=, ++)
Shortcuts that change a variable in place: `x += 5` means `x = x + 5`, and `x++` adds 1 (`x--` takes 1 away).
SKU (stock-keeping unit)
A short code a shop gives each product, such as `MUG-BL` for the speckled blue mug. Because each SKU is unique, it works well as a map key for looking up things like stock counts.
Slice
An ordered list of values of one type, indexed from 0 and grown with `append`. `s[1:3]` makes a new view onto the same array.
slices package
A standard-library package of slice helpers such as `Sort`, `Contains`, `Max`, `Min` and `Clone` (which makes an independent copy).
Slicing
`s[low:high]` gives the part of a slice from index `low` up to, but not including, `high`. The result shares the same array, so changes show through both.
Source code
The text of a program, written in `.go` files, which the compiler turns into an executable.
Standard library
The hundreds of ready-made packages that come with Go, such as `fmt`, `strings` and `os`.
strconv
The standard-library package, short for "string conversion", that converts between text and numbers with `Atoi`, `Itoa` and `ParseFloat`.
String
A piece of text in double quotes. The quotes tell Go it is text, not an instruction.
Stringer
A small interface in `fmt` with one method, `String() string`. If a value has it, `fmt` calls it to get the text to print.
strings package
The standard-library package for working on text. Its functions, like `TrimSpace`, `Split`, `Join` and `Fields`, return a new result and never change the original.
Struct
A type that groups related named fields into one value, like `Product` with SKU, name, price and stock.
Struct literal
A struct value written in code, such as `Product{SKU: "MUG-BL"}`. Fields you leave out get their zero value.
switch
A statement that runs the first `case` matching a value, or `default` if none match. A `switch` with no value tests each case as a condition.

T

t.Errorf and t.Fatalf
Methods on `t *testing.T` that report a failed check. `t.Errorf` marks the test failed and carries on, while `t.Fatalf` fails and stops the test at once.
Table-driven test
A test that puts its cases in a slice of structs and loops over them. It includes the edges of each rule, such as $25 and $50 for shipping.
Terminal
A window where you type instructions instead of clicking. On macOS it's Terminal, and on Windows it's PowerShell.
Test
A `TestXxx(t *testing.T)` function in a `_test.go` file that calls your code with known inputs and checks the results. `t.Errorf` marks failure and carries on, while `t.Fatalf` fails and stops.
time package
The standard-library package for dates and times. `Format` takes a layout written with Go's reference date (Monday, January 2, 2006 at 15:04), and `UTC` is world time.
Type
What kind of value something is and what you can do with it, such as `int`, `string` or `Product`. Go checks types before the program runs.
Type conversion
Writing a type's name like a function to change a value's type, such as `float64(bags)` or `int(3.9)`. Go never mixes `int` and `float64` on its own, and `int(3.9)` cuts the decimals to give 3.

U

Unexported
A name starting with a lowercase letter, so only its own package can use it, like `products` in `catalog`.
Unused variables and imports
Go refuses to compile a program that imports a package or declares a variable and never uses it, so leftovers and typos get caught.

V

var
Keyword for declaring a variable where you name the type yourself and may leave out the value, which then gets the zero value.
Variable
A name that stores a value. `:=` creates one, and `=` changes it.
Verb
A `%` code in a `Printf` text, such as `%d` for a whole number or `%.2f` for two decimals. Values after the text fill the verbs in order, and `%w` in `fmt.Errorf` wraps an error.
VS Code
A free code editor that colors your code and underlines mistakes as you type. With its Go extension installed, it adds support for writing Go.

W

Wrap
To put one error inside another with `%w` in `fmt.Errorf`, adding context while keeping the original, so `errors.Is` and `errors.As` can still find it.

Z

Zero value
The value a variable gets when you don't give one: `0` for numbers, `""` for strings, `false` for bools and `nil` for pointers, slices and maps.