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.
| Name | Description |
|---|---|
append | Add elements to the end of a slice, growing its backing array when needed; always assign the result back. |
make | Create and initialise a slice, map or channel, optionally with a length and capacity (slices) or buffer size (channels). |
len and cap | len returns the number of elements in a string, slice, map, array or channel; cap returns a slice's or channel's capacity. |
min and max | Return 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.
| Name | Description |
|---|---|
fmt.Printf | Print formatted output using verbs like %v, %+v, %d, %s, %q, %.2f and %T, with width and alignment options. |
fmt.Errorf | Create 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.Join | Split a string into a slice around a separator, and join a slice of strings with a separator. |
strings.Contains, HasPrefix and Index | Check whether a string contains, starts or ends with a substring, and find its position. |
strings.Builder | Build a string efficiently piece by piece without creating a new string for every concatenation. |
strconv.Atoi, Itoa and ParseFloat | Convert between strings and numbers or booleans, with errors for invalid input. |
regexp.MustCompile | Compile 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.
| Name | Description |
|---|---|
slices.Sort and slices.SortFunc | Sort a slice in place in ascending order, or with a comparison function for custom and multi-key ordering. |
slices.Contains, Index and BinarySearch | Search slices: check membership, find an index, or binary-search a sorted slice. |
maps.Keys and slices.Sorted | Iterate 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 Unmarshal | Convert 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.
| Name | Description |
|---|---|
errors.Is and errors.As | Check whether an error chain contains a specific error value (Is) or an error of a specific type (As). |
context.WithTimeout and WithCancel | Derive 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 Format | Represent 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.
| Name | Description |
|---|---|
sync.WaitGroup | Wait 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.Once | Protect shared data so only one goroutine accesses it at a time, and run initialisation exactly once. |
chan, select and close | Send 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.
| Name | Description |
|---|---|
os.ReadFile and os.WriteFile | Read or write a whole file in one call, with Unix permissions for new files; errors wrap fs.ErrNotExist and friends. |
bufio.Scanner | Read input line by line (or word by word) from a file, string or stdin without loading it all into memory. |
http.ServeMux and HandleFunc | Route HTTP requests by method and path pattern (with {wildcards}) to handler functions using the standard library. |
log/slog | Write structured logs as key-value pairs in text or JSON, with levels and shared attributes. |