Skip to content

Standard Library

Go’s batteries-included standard library

Section titled “Go’s batteries-included standard library”

One of Go’s defining characteristics is that the language ships with a comprehensive standard library — no npm install, no pip install, no Maven coordinates required for the vast majority of everyday tasks. The library covers HTTP servers and clients, JSON encoding, cryptography, file I/O, text processing, concurrency primitives, and much more. This philosophy keeps dependency trees shallow, binaries self-contained, and upgrade risks low.

The table below shows what this module covers and which package delivers it.

TopicPackage(s)Lesson
Formatted I/O, text, numbersfmt, strings, strconvfmt & strings
Dates, durations, timerstimetime
JSON marshaling / unmarshalingencoding/jsonencoding/json
Streaming I/O, scanningio, bufioio & bufio
Files, environment, exit codesosused throughout this module (see io & bufio) — no dedicated lesson
Error values, wrapping, panicserrorsErrors & Generics module
Cancellation, deadlines, request-scoped valuescontextConcurrency → context
Structured logginglog/slognot covered in depth in this course
HTTP servers and clientsnet/httpnet/http
Mutexes, WaitGroup, OncesyncConcurrency → sync-primitives

This module deliberately does not cover log/slog beyond passing mentions — it is large enough to deserve its own treatment, and everything you need to read its docs (interfaces, structs, error handling, io.Reader/io.Writer) is covered here and in Errors & Generics.

Real Go programs reach for fmt.Println, strings.Split, time.Now, and json.Marshal within the first few lines. Understanding these packages deeply means you will recognise idiomatic Go in any codebase you encounter, and you will reach for the right tool instead of pulling in an extra dependency.

The snippet below touches all four areas in a single main function. Read the output, then trace each call back to its package.

package main
import (
"encoding/json"
"fmt"
"strings"
"time"
)
func main() {
// fmt — formatted output
fmt.Println("Hello, Go!")
fmt.Printf("Pi is approximately %.4f\n", 3.14159)
msg := fmt.Sprintf("2 + 2 = %d", 4)
fmt.Println(msg)
// strings — text manipulation
s := "the quick brown fox"
fmt.Println(strings.ToUpper(s))
fmt.Println(strings.Contains(s, "fox"))
fmt.Println(strings.Join(strings.Split(s, " "), "-"))
// time — fixed date for deterministic output
t := time.Date(2024, 1, 2, 15, 4, 5, 0, time.UTC)
fmt.Println(t.Format("2006-01-02"))
// encoding/json — marshal a struct
type Point struct {
X int `json:"x"`
Y int `json:"y"`
}
p := Point{X: 3, Y: 7}
b, _ := json.Marshal(p)
fmt.Println(string(b))
}
What you getBenefitCost
Batteries-included standard libraryNo external package install for basic tasksOpinionated API — less flexible than third-party alternatives
encoding/json (reflection-based)No code generation stepSlower than a code-gen approach such as easyjson
fmt uses reflectionPrint any valueSlower than a structured logger in production
net/http built inDeploy an HTTP server with zero dependenciesnet/http gained method + wildcard routing in 1.22 (GET /items/{id}) — third-party routers now mostly add value through their middleware ecosystem
  • The standard library fits every use case — some jobs still need a third party: log/slog has been in the standard library since 1.21, but zap/zerolog (alternative structured logging), chi/gin (advanced routing), and gorm (an ORM) remain third-party.
  • fmt.Println is too slow for production — fmt only becomes a problem when logging is on a hot path; benchmark before you optimize.
  • encoding/json is the only option for JSON in Go — jsoniter, easyjson, and sonic are 2-5x faster for high-throughput use cases.

💡 Real-world example

Kubernetes relies mainly on the standard library — net/http for the API server, encoding/json for resource serialization, time for leases and timeouts.

Docker ships as a single binary with no runtime to install — possible because Go compiles a static binary with the standard library baked in.

Prometheus uses net/http for its /metrics endpoint and time for the scrape interval — no external HTTP framework needed.

Which package provides fmt.Sprintf?
Go's standard library is designed so that most everyday programs need:
Which package would you use to convert the string "42" to the integer 42?
What does json.Marshal return when it succeeds?