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.
| Topic | Package(s) | Lesson |
|---|---|---|
| Formatted I/O, text, numbers | fmt, strings, strconv | fmt & strings |
| Dates, durations, timers | time | time |
| JSON marshaling / unmarshaling | encoding/json | encoding/json |
| Streaming I/O, scanning | io, bufio | io & bufio |
| Files, environment, exit codes | os | used throughout this module (see io & bufio) — no dedicated lesson |
| Error values, wrapping, panics | errors | Errors & Generics module |
| Cancellation, deadlines, request-scoped values | context | Concurrency → context |
| Structured logging | log/slog | not covered in depth in this course |
| HTTP servers and clients | net/http | net/http |
| Mutexes, WaitGroup, Once | sync | Concurrency → 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.
Why standard library first?
Section titled “Why standard library first?”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.
A runnable tour
Section titled “A runnable tour”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))} Trade-offs
Section titled “Trade-offs”| What you get | Benefit | Cost |
|---|---|---|
| Batteries-included standard library | No external package install for basic tasks | Opinionated API — less flexible than third-party alternatives |
encoding/json (reflection-based) | No code generation step | Slower than a code-gen approach such as easyjson |
fmt uses reflection | Print any value | Slower than a structured logger in production |
net/http built in | Deploy an HTTP server with zero dependencies | net/http gained method + wildcard routing in 1.22 (GET /items/{id}) — third-party routers now mostly add value through their middleware ecosystem |
Common Misconceptions
Section titled “Common Misconceptions”- The standard library fits every use case — some jobs still need a third party:
log/sloghas 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/httpfor the API server,encoding/jsonfor resource serialization,timefor 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/httpfor its/metricsendpoint andtimefor the scrape interval — no external HTTP framework needed.