Go
04 / 04

Go Interview Questions

Go Interview Questions

  • Goroutines vs threads? Goroutines are lightweight (2KB stack, grows as needed) vs OS threads (1MB+). Scheduled by Go runtime (M:N threading), not OS. Millions can run concurrently. Communicate via channels, not shared memory

  • What is a channel? Typed conduit for goroutine communication. Unbuffered: send blocks until receive (synchronization). Buffered: send blocks only when full. Close to signal no more values — range over channel until closed

  • How to avoid goroutine leaks? Use context.WithCancel/Timeout. Select on done channel. Never start a goroutine without knowing how it will stop. Use errgroup for coordinated cancellation

  • Interfaces in Go? Satisfied implicitly — no "implements" keyword. Any type with matching methods satisfies the interface. Empty interface{} (any) accepts all types. Small interfaces preferred (io.Reader = one method)

  • Error handling philosophy? Errors are values, returned explicitly (not thrown). Check every error. Wrap with fmt.Errorf("context: %w", err). Unwrap with errors.Is() / errors.As(). Use panic only for unrecoverable states

  • Difference between make and new? new(T): allocates zero-value T, returns *T. make(T, args): for slices/maps/channels only, initializes internal structure, returns T (not pointer). Use make for those three types, new rarely

  • How does garbage collection work? Concurrent tri-color mark-and-sweep GC. Runs concurrently with program. Very low pause times (<1ms). Use sync.Pool to reduce allocations. Profile with pprof to find alloc hotspots

  • When to use mutex vs channel? Mutex: protecting shared state (counter, cache). Channel: passing data between goroutines, signaling. Rule: "Share memory by communicating" — channels. But mutexes are fine for simple state protection

Keep your own version of these notes — editable, searchable, and organised by your stack.

Start free