Standard Library

Standard Library Overview

Learn how to discover and combine Go's built-in packages to solve real problems.

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

ProblemUseful packages
Print and format valuesfmt
Work with textstrings, strconv, unicode, regexp
Read and write dataio, os, bufio
Encode structured dataencoding/json
Build HTTP clients and serversnet/http
Work with timetime
Carry cancellation and deadlinescontext
Create and inspect errorserrors
Sort and searchsort, slices, maps
Test codetesting, httptest
Log structured eventslog/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.Reader produces bytes;
  • io.Writer consumes bytes;
  • error describes failure;
  • context.Context communicates 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

  1. Read the package overview.
  2. Find the main types and interfaces.
  3. Read one or two examples.
  4. Write the smallest useful program with the package.
  5. 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.

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.

Copyright © 2026