Standard Library Overview
What you will learn
- find the standard packages used most often;
- read package APIs through their interfaces and examples;
- track resource ownership;
Go's standard library is the set of packages shipped with the language. It covers text, files, JSON, HTTP, cryptography, testing, concurrency, and many other everyday needs.
Learning it does not mean memorising every package. Learn the common building blocks and practise finding the rest.
Packages to learn first
| Problem | Useful packages |
|---|---|
| Print and format values | fmt |
| Work with text | strings, strconv, unicode, regexp |
| Read and write data | io, os, bufio |
| Encode structured data | encoding/json |
| Build HTTP clients and servers | net/http |
| Work with time | time |
| Carry cancellation and deadlines | context |
| Create and inspect errors | errors |
| Sort and search | sort, slices, maps |
| Test code | testing, httptest |
| Log structured events | log/slog |
Read documentation locally
go doc strings
go doc strings.Contains
go doc works directly from the terminal. The online package index at pkg.go.dev/std provides the same API documentation with search and links.
Learn interfaces, not just functions
Many packages connect through small interfaces:
io.Readerproduces bytes;io.Writerconsumes bytes;errordescribes failure;context.Contextcommunicates cancellation and deadlines.
A file, HTTP response body, compressed stream, and in-memory buffer can all be readers. Once you understand the interface, many packages begin to fit together.
func countBytes(r io.Reader) (int64, error) {
return io.Copy(io.Discard, r)
}
This function does not care where the bytes come from.
A method for learning a package
- Read the package overview.
- Find the main types and interfaces.
- Read one or two examples.
- Write the smallest useful program with the package.
- Check how errors, resource cleanup, and cancellation work.
Resource ownership
Many standard-library values hold operating-system resources. When a function successfully opens a file, response body, or connection, the caller usually must close it.
file, err := os.Open("notes.txt")
if err != nil {
return err
}
defer file.Close()
Always read the documentation to learn who owns a resource and when it must be released.
Next: Text and Formatting.
Practice
Choose os, io, or net/http. Identify one resource the caller must close and one error it must handle.
os.Open, the caller handles the returned error and closes the returned file, commonly with defer file.Close() after the error check. For an HTTP client response, the caller closes response.Body after a successful request.Summary
- Prefer the standard library when it clearly meets the requirement.
- Learn package contracts, ownership, and errors—not isolated function names.
- Documentation examples become useful experiments when you change and test them.
Ready to wrap up?
Mark this complete when you can explain the main idea and finish the practice task.