Go Interview Preparation
This chapter covers the most common Go interview topics, coding challenges, and system design questions you’ll encounter.Language Fundamentals
Common Interview Questions
What are the zero values in Go?
What are the zero values in Go?
| Type | Zero Value |
|---|---|
int, float64 | 0 |
string | "" (empty string) |
bool | false |
pointer, slice, map, channel, function, interface | nil |
struct | All fields set to their zero values |
array | All elements set to their zero values |
What's the difference between arrays and slices?
What's the difference between arrays and slices?
Arrays:
- Fixed size, part of the type:
[5]int≠[10]int - Value type - copied when passed
- Size known at compile time
- Dynamic size, backed by an array
- Reference type - contains pointer to underlying array
- Has length and capacity
- Can grow with
append()
How does `defer` work?
How does `defer` work?
- Deferred calls are executed in LIFO order when the function returns
- Arguments are evaluated immediately, not when deferred call runs
- Commonly used for cleanup (closing files, unlocking mutexes)
Explain the difference between make and new
Explain the difference between make and new
new(T): Allocates zeroed storage for type T, returns*Tmake(T, args): Creates and initializes slices, maps, channels only
What are interface{} and any?
What are interface{} and any?
interface{} (or any since Go 1.18) is the empty interface that all types implement. Used for:- Generic containers before generics
- JSON unmarshaling
- Printf-style functions
How are methods different from functions?
How are methods different from functions?
Methods have a receiver - they’re bound to a type:Use pointer receivers when:
- You need to modify the receiver
- The receiver is large (avoid copying)
- Consistency with other methods on the type
Explain Go's memory model
Explain Go's memory model
- Go uses garbage collection (concurrent, tri-color mark-and-sweep)
- Stack: Local variables, function calls (fast, automatic)
- Heap: Escaped variables, dynamically sized data (GC managed)
- Escape analysis determines stack vs heap allocation
Concurrency Questions
What are goroutines and how do they differ from threads?
What are goroutines and how do they differ from threads?
Goroutines:
- Lightweight (2KB initial stack)
- Managed by Go runtime
- M:N scheduling (many goroutines on few OS threads)
- Fast context switching
- Can have thousands running
- Heavy (1-2MB stack)
- Managed by OS kernel
- Expensive context switches
- Limited by OS resources
How do channels work?
How do channels work?
Channels are typed conduits for communication between goroutines:
What is a data race and how do you prevent it?
What is a data race and how do you prevent it?
Data race occurs when multiple goroutines access shared data concurrently with at least one write.Prevention methods:
Explain select statement
Explain select statement
select waits on multiple channel operations:- Blocks until one case can proceed
- Random selection if multiple ready
defaultmakes it non-blocking
What is context and how is it used?
What is context and how is it used?
Context carries deadlines, cancellation signals, and request-scoped values:
How do you handle goroutine leaks?
How do you handle goroutine leaks?
Goroutine leaks occur when goroutines are blocked forever. Prevention:
Coding Challenges
Implement a LRU Cache
Implement Rate Limiter
Implement Worker Pool
Implement Concurrent Map
System Design Topics
Design a URL Shortener
- Distributed ID generation (Snowflake, UUID)
- Database sharding by short code prefix
- Caching (Redis) for hot URLs
- Rate limiting per user/IP
Design a Pub/Sub System
Performance Questions
How do you profile a Go application?
How do you profile a Go application?
How do you reduce memory allocations?
How do you reduce memory allocations?
- Pre-allocate slices:
make([]T, 0, expectedSize) - Use
sync.Poolfor temporary objects - Avoid string concatenation in loops (use
strings.Builder) - Reuse buffers
- Use value types instead of pointers when appropriate
- Align struct fields properly
What is escape analysis?
What is escape analysis?
Escape analysis determines whether a variable can stay on the stack or must “escape” to the heap:Stack is faster (no GC), heap is necessary for escaped values.
Best Practices Checklist
Code Quality
- Use
go fmtandgo vet - Run
golangci-lint - Write table-driven tests
- Use meaningful variable names
- Keep functions small and focused
- Handle all errors
- Use interfaces for dependencies
Concurrency
- Prefer channels for communication
- Always use context for cancellation
- Run race detector (
-race) - Close channels from sender side
- Use
sync.WaitGroupfor goroutine synchronization
Performance
- Profile before optimizing
- Pre-allocate slices when size is known
- Use
sync.Poolfor frequent allocations - Avoid defer in hot loops
- Use buffered I/O
Production
- Structured logging
- Health checks
- Graceful shutdown
- Configuration management
- Metrics and monitoring
- Proper error handling with stack traces
Quick Reference Card
Summary
Mastering Go for interviews requires:- Strong fundamentals: Types, interfaces, error handling
- Concurrency expertise: Goroutines, channels, sync primitives
- Performance awareness: Profiling, memory management
- Production experience: Logging, config, deployment
- Coding practice: LeetCode-style problems in Go
- System design: Distributed systems patterns