Skip to content
Tutorials Center
My learning

Topics

Lesson 147 of 147 ยท Reference overview

Go Reference: Keywords, Built-ins and Packages

A map of the Go language and standard library - the 25 keywords, predeclared types and built-in functions, operator precedence, and about 65 standard packages grouped by job with links to lessons.

A tutorial teaches you in order. A reference answers one question quickly: what does this keyword do, which built-in do I need, which package holds this feature? This page is the reference entry point for Go. It lists everything that is part of the language itself (keywords, predeclared identifiers, operators) and then a guided tour of about 65 standard-library packages, grouped by the job they do, each with a link to the lesson that covers it in depth.

Use it as a map. When a row interests you, follow the link; when you already know the area, skim the table and move on. For syntax at a glance, the Go cheat sheet is denser, and the what's new page shows which features arrived in which release.

The 25 keywords

Go keeps its vocabulary small on purpose. There are exactly 25 reserved words, and you cannot use them as names. You can ask the standard library itself, because the go/token package (the compiler's own vocabulary) knows them:

Go Listing the keywords with go/token

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Notice that string and print are not keywords. They are predeclared identifiers (next section) and technically you could shadow them, though you should not. Grouped by purpose, the keywords are:

Purpose Keywords Lessons
Declarations const, var, type, func, import, package Variables, Constants, Packages
Composite types struct, interface, map, chan Structs, Interfaces, Maps, Channels
Branching if, else, switch, case, default, fallthrough If/else, Switch
Looping for, range, break, continue For loops, Range
Jumping goto (with labels), return Labels and goto
Concurrency go, select Goroutines, Select
Deferred work defer Defer

There is no while, do, class, try, new-as-keyword or enum. for covers every loop, and new is a function.

Predeclared identifiers

These names are available in every file without an import. They live in the "universe" scope, the outermost scope of a Go program.

Types: bool, string, error, any, comparable, byte (alias of uint8), rune (alias of int32), int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr, float32, float64, complex64, complex128. See basic types and numbers.

Constants: true, false, iota. Zero value for reference-like types: nil.

Built-in functions:

Function What it does Applies to
len(x) Number of elements or bytes strings, arrays, slices, maps, channels
cap(x) Capacity arrays, slices, channels
append(s, v...) Adds elements, may reallocate; returns the new slice slices
copy(dst, src) Copies elements; returns the count slices (and string to []byte)
clear(x) Deletes all map entries, zeroes all slice elements maps, slices
delete(m, k) Removes a key (no-op if absent) maps
make(T, ...) Creates an initialized slice, map or channel slices, maps, channels
new(T) Allocates a zero T, returns *T; since Go 1.26 it also accepts an initial value any type
min(a, b...), max(a, b...) Smallest/largest argument (Go 1.21) ordered types
close(ch) Closes a channel channels
panic(v), recover() Start and stop a panic see panic and recover
complex, real, imag Build and take apart complex numbers complex types
print, println Primitive output to standard error; for bootstrapping only, use fmt instead basic types

The details are in make and new, slices and maps. Here is a tour of the most common ones; its output is exactly what the comments say:

Go Built-in functions in action

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Operators and precedence

Go has five levels of binary operator precedence, and they are small enough to memorize. Higher numbers bind tighter:

Precedence Operators
5 * / % << >> & &^
4 + - | ^
3 == != < <= > >=
2 &&
1 ||

Unary operators (+ - ! ^ * & <-) bind tighter than any binary operator. There is no ternary ?:, no ++ or -- in expressions (they are statements), and no operator overloading. The compiler's own tables confirm the numbers:

Go Precedence straight from go/token

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

More on operators, including bit manipulation, is in the operators lesson.

Standard library by job

The standard library is large, but you meet the same few dozen packages again and again. Import paths are shown exactly as you write them.

Text, strings and bytes

Package Purpose Learn more
fmt Formatted printing, scanning, Stringer fmt in depth
strings Search, split, join, replace, Builder, Reader strings package
strconv Convert to and from strings (Atoi, ParseFloat, Quote) type conversion
bytes The strings toolbox for []byte, plus Buffer bytes and buffers
unicode, unicode/utf8, unicode/utf16 Character classes, rune encoding and decoding Unicode and text
regexp RE2 regular expressions, linear time regexp
text/template, html/template Templates; the HTML one escapes for safety text templates
text/tabwriter Aligned columns of text fmt in depth

Input, output and the file system

Package Purpose Learn more
io Reader, Writer, Copy, Pipe, MultiWriter os, io and bufio
bufio Buffered readers, writers and Scanner os, io and bufio
os Files, environment, arguments, exit files and paths
io/fs The read-only file-system interface files and paths
path, path/filepath Slash paths and OS paths files and paths
embed Put files into the binary with //go:embed files and paths
os/exec, os/signal Run other programs; react to signals flag and CLI
flag Command-line flags flag and CLI
log, log/slog Simple and structured logging logging

Data structures and algorithms

Package Purpose Learn more
slices, maps Generic helpers: sort, search, clone, compare, keys, values slices and maps packages
sort Older sorting API, still common sorting
iter The Seq and Seq2 iterator types iterators
container/heap, container/list, container/ring Priority queue, doubly linked list, circular list slices and maps packages
unique Canonicalize ("intern") comparable values whats new in Go
math, math/bits, math/cmplx Floating-point functions, bit tricks, complex math math and random
math/big Arbitrary-precision integers, rationals and floats numbers
math/rand/v2 Pseudo-random numbers (not for secrets) math and random
time Instants, durations, formatting, timers time

Encoding, compression and cryptography

Package Purpose Learn more
encoding/json JSON with struct tags JSON
encoding/xml, encoding/csv XML and CSV CSV, XML and other formats
encoding/binary, encoding/gob Fixed-layout binary and Go-to-Go streams CSV, XML and other formats
encoding/base64, encoding/hex Text encodings of bytes crypto and hashing
compress/gzip, archive/zip, archive/tar Compression and archives CSV, XML and other formats
crypto/sha256, crypto/hmac, crypto/rand Hashes, message authentication, secure randomness crypto and hashing
crypto/aes, crypto/cipher Symmetric encryption crypto and hashing
crypto/tls, crypto/x509 TLS connections and certificates crypto and hashing
hash/fnv, hash/crc32, hash/maphash Non-cryptographic hashes crypto and hashing

Networking and the web

Package Purpose Learn more
net TCP, UDP, listeners, DNS TCP and protocols
net/http HTTP client and server, ServeMux HTTP server, HTTP client
net/http/httptest Fake servers and recorders for tests handlers and middleware
net/url, net/netip URL parsing; compact IP address types URLs and HTTP basics
net/rpc, net/mail, net/smtp Older RPC and mail helpers gRPC and RPC
database/sql One interface for SQL databases, with drivers database/sql

Concurrency, runtime and the language machinery

Package Purpose Learn more
context Cancellation, deadlines, request-scoped values context
sync Mutex, RWMutex, WaitGroup, Once, Pool, Map sync package
sync/atomic Lock-free counters and typed atomic values atomics
errors New, Is, As, Join, Unwrap errors, wrapping errors
runtime, runtime/debug Scheduler, GC settings, stack traces scheduler internals
runtime/pprof, net/http/pprof, runtime/trace Profiling and tracing profiling and debugging
reflect Inspect and build values at run time reflection
unsafe Sizes, offsets and pointer conversions unsafe and cgo
go/ast, go/parser, go/token, go/format Read and write Go source code tooling basics

Testing

Package Purpose Learn more
testing Tests, benchmarks, fuzzing, subtests, t.Cleanup testing, benchmarks and fuzzing
testing/fstest, testing/iotest, testing/quick Fake file systems, odd readers, property checks table-driven tests

Where to look something up

A habit worth building: before writing a helper, search the standard library. Go's library has strings.Cut, slices.Sort, maps.Keys, sync.OnceValue, strings.SplitSeq and many other small functions that replace code people used to hand-write.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. How many keywords does Go reserve?

    Go has 25 keywords. Names such as string, len, nil and true are predeclared identifiers, not keywords.
  2. 2. Which of these is NOT a Go keyword?

    Go has no while loop; for covers every loop form. range, defer and fallthrough are all keywords.
  3. 3. What does clear(m) do for a map m?

    clear (Go 1.21) deletes all entries of a map. For a slice it zeroes every element but keeps the length.
  4. 4. What is the value of 1 + 2<<1?

    << has precedence 5, higher than + at 4, so the expression is 1 + (2<<1) = 1 + 4 = 5.
  5. 5. Which package would you reach for to run a function with a deadline and cancel it from outside?

    context carries cancellation signals and deadlines through call chains; sync provides locks and wait groups but not cancellation.

Summary

  • Go has 25 keywords; everything else, including string, len, nil and true, is a predeclared identifier in the universe scope.
  • The built-in functions are append, cap, clear, close, complex, copy, delete, imag, len, make, max, min, new, panic, print, println, real and recover.
  • Binary operators have five precedence levels; *, /, %, <<, >>, &, &^ bind tightest and || loosest.
  • The standard library groups naturally: text, I/O, data structures, encoding and crypto, networking, concurrency and runtime, testing.
  • Use go doc and pkg.go.dev for exact signatures, and the lessons linked above for explanations and runnable examples.

You have reached the end of this track's reference material. Continue with the Go cheat sheet or test yourself with the exercises.