Text preview & study summary
Go (Golang) Programming Basics
A free sample of 5 questions from this quiz, shown in full with answer choices and explanations. No interactivity — everything is visible on this page for study and review.
Want to test your knowledge? Launch the Interactive Exam Simulator
Question 1
Go uses garbage collection for memory management, so developers do not need to manually free memory allocated with `new()` or `make()`.
Explanation
Go has a concurrent, tri-color mark-and-sweep garbage collector. Developers allocate memory with `new(Type)` (returns pointer to zero-valued type) or `make(Type, args)` (initializes slices, maps, channels). The GC automatically reclaims memory when objects are no longer reachable. Go's GC has low-latency design (sub-millisecond pauses since Go 1.14+). While garbage collected, developers should still be mindful of: memory leaks from goroutines blocking forever, large allocations, and keeping references to unneeded data. `runtime/pprof` and `go tool pprof` help profile memory usage. Go also allows unsafe pointer operations but they should be avoided.
Question 2
Which Go concurrency mechanism should be used when a function needs to wait for multiple goroutines to complete before proceeding?
Explanation
`sync.WaitGroup` coordinates multiple goroutines:
```go
var wg sync.WaitGroup
for _, url := range urls {
wg.Add(1) // increment counter
go func(u string) {
defer wg.Done() // decrement on exit
fetchURL(u)
}(url)
}
wg.Wait() // block until counter reaches 0
fmt.Println("All fetches complete")
```
`Add(n)` increments the counter (call before starting goroutines). `Done()` decrements by 1 (use with `defer` for safety). `Wait()` blocks until counter reaches 0. Alternatives: using a done channel (`ch := make(chan struct{}, n); for range n { <-ch }`); `errgroup.Group` (golang.org/x/sync) for goroutines that return errors. WaitGroup is the idiomatic choice for "fan-out, wait-for-all" patterns.
Question 3
What is the zero value for each Go type, and why is it important?
Explanation
Go's zero value guarantee: all newly declared variables are automatically initialized to their type's zero value — no uninitialized memory. Zero values: `int/float64` = 0, `bool` = false, `string` = "", `pointer` = nil, `slice` = nil (but usable with append), `map` = nil (reading returns zero, writing panics — must initialize with `make`), `channel` = nil (blocking), `interface` = nil, `struct` = all fields zero-valued. Importance: no undefined behavior from uninitialized variables, safe default values, enables patterns like `var mu sync.Mutex` (ready to use without initialization). `var wg sync.WaitGroup` works immediately — zero value is ready.
Question 4
What is the Go `context` package primarily used for in network applications?
Explanation
`context.Context` is essential for Go server applications: `context.WithCancel(parent)` — creates cancelable context; calling `cancel()` signals all goroutines watching `ctx.Done()` channel to stop. `context.WithTimeout(parent, duration)` — auto-cancels after duration (useful for HTTP client timeouts). `context.WithDeadline(parent, time)` — cancels at specific time. `context.WithValue(parent, key, value)` — carries request-scoped data (request ID, auth token). Pattern: pass context as first argument to all functions that do I/O: `func FetchData(ctx context.Context, url string) error`. HTTP servers automatically create a request context: `r.Context()`. Context prevents goroutine leaks by enabling cooperative cancellation.
Question 5
What is a goroutine in Go?
Explanation
Goroutines are Go's concurrency primitive — lightweight "green threads" managed by the Go runtime (not OS threads). Starting one: `go functionName(args)` or `go func() { ... }()`. The Go runtime multiplexes goroutines onto OS threads using an M:N threading model. Goroutines start with ~2KB stack (can grow to gigabytes if needed) vs OS thread (~1-8 MB), enabling thousands/millions of concurrent goroutines. Goroutines communicate via channels (`chan` type) following the principle: "Don't communicate by sharing memory; share memory by communicating." This is Go's CSP (Communicating Sequential Processes) model.
