Go Standard Library Flashcards
Go Standard Library Flashcards
25 flashcards covering key packages in the Go standard library, fmt, strings, io, net/http, encoding/json, and os.
Additional Flashcard Topics
fmt Package:
fmt.Printf, fmt.Sprintf, fmt.Errorf. Format verbs:%v(default),%+v(struct fields),%#v(Go syntax),%T(type).Errorfcreates formatted errors.strings Package:
strings.Contains, strings.Split, strings.Join, strings.TrimSpace.strings.Builderfor efficient string concatenation.io Package:
io.Readerandio.Writerare the foundational interfaces.io.Copy(dst, src)streams data.io.TeeReadersplits a reader into two.net/http Package:
http.ListenAndServe(":8080", nil)starts a server.http.HandleFuncregisters route handlers.http.Clientwith timeouts for production use.encoding/json Package:
json.Marshal/json.Unmarshalfor JSON. Struct tags control field names: ,json:"name,omitempty", .json.RawMessagefor deferred parsing.os Package:
os.Args, os.Getenv, os.Open, os.Create.os.Exitterminates the program.os.Signalhandles OS signals.
Intuition
Go’s standard library is remarkably complete, fmt handles formatted I/O, strings provides text manipulation, io defines the foundational Reader/Writer interfaces that compose like Unix pipes, net/http offers a production-ready HTTP server and client, encoding/json handles JSON marshalling/unmarshalling, and os interacts with the operating system. The key philosophy: small, composable interfaces that snap together. Go’s standard library is intentionally minimal, it provides the building blocks, not the entire framework.
Common Pitfalls
- Goroutine leaks: Starting goroutines without a cancellation mechanism (context or done channel), they run forever, consuming memory. Always use
context.Contextfor cancellation. - Map concurrency: Maps in Go are not safe for concurrent read/write, using them from multiple goroutines without a mutex causes a runtime panic. Use
sync.Mapor wrap withsync.RWMutex. - Defer evaluation timing:
deferarguments are evaluated when thedeferstatement executes, not when the deferred function runs, this often surprises developers with pointer semantics. - Error wrapping:
fmt.Errorf("context: %w", err)wraps errors;%vdoes not. Unwrapping witherrors.Isanderrors.Asrequires%w. - String vs []byte:
stringis immutable;[]byteis mutable. Converting between them copies data.unsafe.Pointercan avoid copying but is unsafe.
Cross-References
- I/O: Deep dive into io.Reader, io.Writer, and buffered I/O covered in these flashcards.
- net/http: HTTP server and client patterns using the standard library.
- Go Practice: Auto-graded problems testing core Go concepts alongside standard library usage.
- Rust Basics: Ownership model compared to Go’s garbage collection.