Go glossary

The essential Go terms defined clearly: goroutine, channel, slice, interface, pointer and more.

Channel

A typed conduit to send and receive values between goroutines.

A channel is created with make(chan T). Sending (ch <- v) and receiving (v := <-ch) synchronise goroutines. An unbuffered channel blocks until both a sender and a receiver are ready, making it a synchronisation tool as much as a communication one.

                ch := make(chan int)
              
error

The built-in interface used to signal that an operation failed.

error is an interface with a single method Error() string. In Go, errors are values returned explicitly, usually as a function's last return, and checked with if err != nil. You wrap an error with fmt.Errorf and %w.

                if err != nil { return err }
              
gofmt

The official tool that automatically formats Go code.

gofmt applies a single canonical style to all Go code, ending formatting debates. The go fmt command runs gofmt over a package. The code shown on LearnGoLab is formatted with gofmt.

                gofmt -w main.go
              
Goroutine

A function running concurrently, scheduled by the Go runtime.

A goroutine is started with the go keyword. It is far lighter than an OS thread: the Go runtime multiplexes thousands of them onto a small number of threads. Goroutines usually communicate through channels rather than by sharing memory.

                go processTask(id)
              
Interface

A set of method signatures a type can satisfy implicitly.

In Go an interface is satisfied as soon as a type has all the required methods: there is no implements keyword. The empty interface interface{} (or any) is satisfied by every type. Interfaces enable decoupled, testable code.

                type Reader interface { Read(p []byte) (int, error) }
              
Map

A table that associates keys with values.

A map is created with make(map[K]V) or a literal. Reading a missing key returns the value type's zero value; the value, ok := m[k] form distinguishes absence from a zero value. Maps are not safe for concurrent writes.

                ages := map[string]int{"Ada": 36}
              
Pointer

A value that holds the memory address of another value.

&x takes the address of x; *p dereferences the pointer p. Go has no pointer arithmetic and its garbage collector manages memory. Pointers are mostly used to share and mutate a value without copying it.

                p := &value
              
Slice

A resizable view over an underlying array.

A slice describes a segment of an array through a pointer, a length and a capacity. append can grow a slice, reallocating the backing array when needed. Slices are the most common sequence type in Go.

                nums := []int{1, 2, 3}
              
Struct

A composite type that groups named fields.

A struct groups related data. You attach methods to it through a receiver. Structs are copied by value; to mutate a struct inside a method, use a pointer receiver.

                type Point struct { X, Y int }
              
Zero value

The default value given to a variable declared without initialisation.

Every type has a zero value: 0 for numbers, "" for string, false for bool, nil for pointers, slices and maps. Because of it, there are no uninitialised variables in Go.

                var count int // 0
              

Search

Search runs entirely in your browser.