Skip to content

Go Standard Library Flashcards

Go Standard Library Flashcards

15 flashcards covering key packages in the Go standard library, fmt, strings, io, net/http, encoding/json, and os.


Intuition

Go’s standard library is deliberately comprehensive, the language philosophy batteries included. fmt is formatted I/O (like C’s printf), strings handles text manipulation, io defines the fundamental interfaces (Reader, Writer) that compose like pipelines, and net/http provides a production-ready HTTP server and client. The key insight is that Go interfaces are satisfied implicitly: any type with the right methods automatically implements the interface, no implements keyword needed.

Common Pitfalls

  • Ignoring errors: Go returns (value, error) tuples, discarding the error with _ hides failures that will bite you later. Always check err != nil.
  • String indexing: s[i] returns a bytenot a rune, iterating over a string by index breaks for multi-byte UTF-8 characters. Use for _, r := range s instead.
  • Deferred function arguments: Arguments to defer are evaluated immediately when the statement executes, not when the function returns, this catches many developers off guard.
  • HTTP client timeouts: The default http.Client has no timeout, requests can hang forever. Always set Timeout on production HTTP clients.
  • JSON struct tags: Forgetting json:"name" tags means Go uses the field name as-is (capitalised), which may not match the expected API field name. Always add explicit tags.

Study Tips

  • Read the standard library source: Go’s standard library is well-documented and readable. Reading fmt, stringsand io source code teaches idiomatic Go patterns.
  • Use go doc and godoc: Run go doc fmt.Println or godoc -http=:6060 to browse documentation offline. The standard library docs are comprehensive.
  • Practice error handling patterns: Write functions that return (T, error) and handle each error explicitly. This builds the discipline that prevents silent failures in production.
  • Write HTTP servers with net/http: Build a simple REST API using only the standard library. This demonstrates Go’s approach to web development without external dependencies.
  • Explore the testing package: Use testing.T and table-driven tests to verify your code. Go’s testing framework is built into the language and encourages thorough coverage.
  • Practise with io.Reader and io.Writer interfaces: These interfaces are the foundation of Go’s I/O model. Write programs that read from files, write to network connections, and pipe data through multiple transformations.
  • Study the context package: Context controls cancellation, timeouts, and request-scoped values in Go programs. Understanding context is essential for writing production-grade Go services.

Cross-References

  • I/O: Detailed notes on file and stream operations in the standard library.
  • Net/HTTP: HTTP client and server implementations using the standard library.
  • Strings and Time: String manipulation and time handling utilities.

Detailed Content

Core Concepts

Worked Examples

Common Mistakes

  • Rushing through foundational material
  • Not practising problems after reading
  • Failing to connect concepts across topics

Further Reading