Key takeaways
- Senior Go interviews are about concurrency done safely: goroutine leaks, channel ownership,
contextcancellation and the race detector. - Expect questions on slices and maps internals, interface nil traps, error wrapping and when generics beat interfaces.
- Be ready to debug a production issue live — memory growth, high CPU or latency — with
pprofand traces.
Experienced Go interviews skip the syntax and go straight to concurrency, correctness and production debugging. These are the Golang interview questions that come up most for developers with 3–8 years of experience, with short answers you can say out loud and code you can write on a whiteboard. For 100+ more, from basics to scenarios, see the full Go interview questions page.
Concurrency
What is a goroutine leak, and how do you prevent one?
A goroutine leak is a goroutine that blocks forever — on a channel send or receive, a lock or a network call — so it is never collected. Leaks show up as steadily growing memory and runtime.NumGoroutine().
The classic cause is a sender whose receiver has gone away:
// LEAK: if ctx times out first, nobody reads ch and the goroutine blocks forever
func fetch(ctx context.Context) (string, error) {
ch := make(chan string)
go func() { ch <- slowCall() }()
select {
case v := <-ch:
return v, nil
case <-ctx.Done():
return "", ctx.Err()
}
}
// FIX: a buffer of 1 lets the goroutine finish even if no one is listening
func fetchFixed(ctx context.Context) (string, error) {
ch := make(chan string, 1)
go func() { ch <- slowCall() }()
select {
case v := <-ch:
return v, nil
case <-ctx.Done():
return "", ctx.Err()
}
}How to find leaks: look at /debug/pprof/goroutine?debug=2 and group the identical stacks, and use go.uber.org/goleak in tests.
Buffered vs unbuffered channels — when do you use each?
- Unbuffered: a send blocks until a receiver is ready — it synchronises the two goroutines. Use it for hand-offs and signals.
- Buffered: sends block only when the buffer is full — it decouples producer and consumer speed. Use it for bounded queues and to avoid leaks like the one above.
Who should close a channel?
The sender (owner) closes it, and only when no more values will be sent. Receivers never close, because sending on a closed channel panics. With multiple senders, use a sync.WaitGroup and close once all senders are done:
func merge(inputs ...<-chan int) <-chan int {
out := make(chan int)
var wg sync.WaitGroup
for _, in := range inputs {
wg.Add(1)
go func(c <-chan int) {
defer wg.Done()
for v := range c {
out <- v
}
}(in)
}
go func() { wg.Wait(); close(out) }() // close after every sender finishes
return out
}Write a worker pool with bounded concurrency and cancellation.
This is one of the most common live-coding tasks for senior Go roles:
func process(ctx context.Context, jobs []int, workers int) ([]int, error) {
g, ctx := errgroup.WithContext(ctx) // golang.org/x/sync/errgroup
g.SetLimit(workers) // at most `workers` goroutines at a time
results := make([]int, len(jobs))
for i, job := range jobs {
g.Go(func() error { // Go 1.22+: i and job are per-iteration
if err := ctx.Err(); err != nil {
return err // stop early if another job failed
}
results[i] = job * job // each goroutine writes its own index: no race
return nil
})
}
return results, g.Wait() // first error cancels ctx for the rest
}Explain the three key points: bounded concurrency (SetLimit), error propagation with cancellation, and no shared writes (each goroutine owns one index).
Mutex vs RWMutex vs atomic — how do you choose?
sync.Mutex: the default for protecting shared state.sync.RWMutex: many readers or one writer. It only helps when reads dominate heavily and the critical sections are not tiny — otherwise its overhead makes it slower than a plain Mutex.sync/atomic(atomic.Int64,atomic.Pointer[T]): a single counter or flag, or swapping a config pointer.- Channels: when you are passing ownership of data, not protecting it.
How do you find and fix a data race?
Run tests and services with go test -race (or go run -race). It reports both conflicting accesses with stack traces. Fix the race by protecting the data with a mutex or atomic, by confining it to one goroutine, or by passing it over a channel. Also remember that maps are not safe for concurrent writes — a concurrent write can crash the program with fatal error: concurrent map writes.
Language internals
What happens when you append to a slice that shares an underlying array?
Slices are a view (pointer, length, capacity) over an array. If there is spare capacity, append writes into the shared array and can overwrite another slice's data:
a := []int{1, 2, 3, 4}
b := a[:2] // len 2, cap 4 — shares a's array
b = append(b, 99) // capacity available: writes into a[2]!
fmt.Println(a) // [1 2 99 4]
c := a[:2:2] // full slice expression: cap limited to 2
c = append(c, 7) // no spare capacity → new array, a untouchedWhy can an interface holding a nil pointer be non-nil?
An interface value is a pair: (type, value). It is only nil when both are nil. A typed nil pointer stored in an interface has a type, so the comparison with nil is false:
type MyErr struct{}
func (*MyErr) Error() string { return "boom" }
func mayFail(fail bool) error {
var e *MyErr // nil pointer
if fail {
e = &MyErr{}
}
return e // BUG: returns (type=*MyErr, value=nil) — not a nil error
}
func main() {
fmt.Println(mayFail(false) == nil) // false!
}
// Fix: return nil explicitly when there is no error.How do you wrap and inspect errors in modern Go?
Wrap errors with fmt.Errorf("...: %w", err) to add context, check a specific error with errors.Is, and extract a typed error with errors.As. errors.Join (Go 1.20+) combines several errors:
var ErrNotFound = errors.New("not found")
func getUser(id int) error {
return fmt.Errorf("getUser %d: %w", id, ErrNotFound)
}
err := getUser(7)
errors.Is(err, ErrNotFound) // true, even through the wrappingWhen should you use generics instead of interfaces?
Use generics when the same algorithm works on different types and you want type safety without conversions: containers, Map/Filter helpers, or numeric functions constrained with cmp.Ordered. Use interfaces when different types behave differently behind one method set (io.Reader, a storage backend). Most business logic still reads best with interfaces. Overusing type parameters is a common code-review complaint.
func Max[T cmp.Ordered](xs []T) (T, bool) {
var zero T
if len(xs) == 0 {
return zero, false
}
m := xs[0]
for _, x := range xs[1:] {
m = max(m, x) // built-in max, Go 1.21+
}
return m, true
}Production questions
How do you implement graceful shutdown for an HTTP server?
Listen for SIGINT/SIGTERM, stop accepting new requests, and give in-flight requests time to finish with Server.Shutdown(ctx):
func main() {
srv := &http.Server{Addr: ":8080", Handler: routes()}
go func() {
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
}()
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
<-ctx.Done() // wait for a shutdown signal
shutdownCtx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
if err := srv.Shutdown(shutdownCtx); err != nil {
log.Printf("forced shutdown: %v", err)
}
}In Kubernetes, also fail the readiness probe first so the load balancer stops sending traffic.
Your Go service's latency suddenly doubled. How do you investigate?
- Check metrics: request rate, error rate, latency percentiles, CPU, memory, GC pause time and goroutine count.
- Profile with
net/http/pprof: a CPU profile (/debug/pprof/profile?seconds=30), a heap profile, and block/mutex profiles for lock contention. - Trace a slow request (OpenTelemetry) to see whether time goes to the database, downstream calls or the service itself.
- Check recent changes: deploys, traffic patterns, dependency latency.
- Common Go culprits: lock contention, excessive allocations driving GC (tune with fewer allocations,
sync.Pool, orGOMEMLIMIT), unbounded goroutines, and missing timeouts on HTTP clients.
Keep practising
- 105 Go questions from basics to scenarios: Go interview questions with answers
- Test yourself: free Go quiz
- Prepare the full loop: programming interview preparation roadmap