Golang Interview Questions and
Answers (3+ Years Experience)
Core Golang Language
What are the differences between var, const, and := in Go?
`var` declares a variable with optional initialization.
`const` declares an immutable constant.
`:=` is short-hand for declaring and initializing inside functions.
Example:
var x int = 5
const pi = 3.14
name := "Go"
What is the zero value of different Go data types?
int → 0
float64 → 0.0
string → ""
bool → false
Pointers, slices, maps, channels, interfaces → nil
What is a defer statement? When is it executed?
`defer` schedules a function call to run after the current function completes.
Used for cleanup like closing files or releasing locks.
Explain how Go handles memory management and garbage collection.
Go uses automatic garbage collection with a concurrent collector,
allowing efficient memory reuse without long pause times.
How do you handle error propagation in Go?
Check `err` after each function call.
Use `errors.New`, `fmt.Errorf`, `errors.Is`, and `errors.As` to handle errors.
Concurrency
What is a goroutine, and how is it different from a thread?
A goroutine is a lightweight thread managed by Go runtime.
It consumes less memory and is scheduled efficiently by Go.
What are buffered vs. unbuffered channels?
Unbuffered: sender and receiver wait for each other.
Buffered: can send/receive without waiting until full/empty.
Explain select in Go. How is it useful in concurrency?
`select` waits on multiple channel operations.
Used for multiplexing, timeouts, and non-blocking communication.
How do you avoid race conditions in Go?
Use `sync.Mutex`, channels, or `sync/atomic`.
Run code with `-race` flag to detect race conditions.
What does sync.WaitGroup do, and how is it used?
It waits for a group of goroutines to finish using `Add()`, `Done()`, and `Wait()`.