Reference · 26 entries

Go Standard Library Reference

A Go cheat sheet: built-ins, fmt, strings, strconv, slices, maps, iterators, JSON, errors, context, time, sync, channels, files, HTTP and slog, with real examples run on Go 1.27.

Go 1.27 Version history

Every example is a complete Go program that was compiled and run with Go 1.27 when this page was built, so the output is real. For step-by-step explanations, see the Go tutorial.

Built-in Functions

Go's built-in functions for slices, maps and values: append, make, len, cap, min and max.

NameDescription
appendAdd elements to the end of a slice, growing its backing array when needed; always assign the result back.
makeCreate and initialise a slice, map or channel, optionally with a length and capacity (slices) or buffer size (channels).
len and caplen returns the number of elements in a string, slice, map, array or channel; cap returns a slice's or channel's capacity.
min and maxReturn the smallest or largest of their arguments for any ordered type (numbers and strings); built in since Go 1.21.

Formatting, Strings and Text

Print and format with fmt, split, search and build strings, convert numbers with strconv, and match regular expressions.

NameDescription
fmt.PrintfPrint formatted output using verbs like %v, %+v, %d, %s, %q, %.2f and %T, with width and alignment options.
fmt.ErrorfCreate an error with a formatted message; the %w verb wraps another error so errors.Is and errors.As can find it.
strings.Split and strings.JoinSplit a string into a slice around a separator, and join a slice of strings with a separator.
strings.Contains, HasPrefix and IndexCheck whether a string contains, starts or ends with a substring, and find its position.
strings.BuilderBuild a string efficiently piece by piece without creating a new string for every concatenation.
strconv.Atoi, Itoa and ParseFloatConvert between strings and numbers or booleans, with errors for invalid input.
regexp.MustCompileCompile a regular expression (RE2 syntax, linear time) and use it to match, find and replace text.

Slices, Maps, Iterators and JSON

Sort and search slices, iterate maps in order, write iterators with iter.Seq, and encode JSON.

NameDescription
slices.Sort and slices.SortFuncSort a slice in place in ascending order, or with a comparison function for custom and multi-key ordering.
slices.Contains, Index and BinarySearchSearch slices: check membership, find an index, or binary-search a sorted slice.
maps.Keys and slices.SortedIterate a map's keys or values; combine with slices.Sorted for a deterministic order, since map iteration is random.
iter.Seq (range over functions)Write iterators as functions that yield values, and loop over them with for range like slices and maps.
encoding/json Marshal and UnmarshalConvert Go values to JSON and back using struct tags to name fields, omit empty values and skip fields.

Errors, Context and Time

Wrap and inspect errors, cancel work with context, and handle durations and time formats.

NameDescription
errors.Is and errors.AsCheck whether an error chain contains a specific error value (Is) or an error of a specific type (As).
context.WithTimeout and WithCancelDerive a context that is cancelled after a timeout or on demand, to stop work and outbound calls that take too long.
time.Duration, Parse and FormatRepresent durations, measure elapsed time, and parse or format times with Go's reference layout (2006-01-02 15:04:05).

Concurrency

Run goroutines with WaitGroup, protect data with Mutex and Once, and communicate with channels and select.

NameDescription
sync.WaitGroupWait for a group of goroutines to finish; WaitGroup.Go (Go 1.25+) starts a goroutine and tracks it in one call.
sync.Mutex and sync.OnceProtect shared data so only one goroutine accesses it at a time, and run initialisation exactly once.
chan, select and closeSend values between goroutines with channels, wait on several with select, and close a channel to end range loops.

Files, HTTP and Logging

Read and write files, scan input line by line, route and call HTTP, and write structured logs with slog.

NameDescription
os.ReadFile and os.WriteFileRead or write a whole file in one call, with Unix permissions for new files; errors wrap fs.ErrNotExist and friends.
bufio.ScannerRead input line by line (or word by word) from a file, string or stdin without loading it all into memory.
http.ServeMux and HandleFuncRoute HTTP requests by method and path pattern (with {wildcards}) to handler functions using the standard library.
log/slogWrite structured logs as key-value pairs in text or JSON, with levels and shared attributes.

Was this page helpful?