Golang Interview Questions and Answers (3+ Years Experience)
Core Golang Language
Q: What are the differences between var, const, and := in Go?
A: `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"
Q: What is the zero value of different Go data types?
A: int -> 0
float64 -> 0.0
string -> ""
bool -> false
Pointers, slices, maps, channels, interfaces -> nil
Q: What is a defer statement? When is it executed?
A: `defer` schedules a function call to run after the current function completes.
Used for cleanup like closing files or releasing locks.
Q: Explain how Go handles memory management and garbage collection.
A: Go uses automatic garbage collection with a concurrent collector,
allowing efficient memory reuse without long pause times.
Q: How do you handle error propagation in Go?
A: Check `err` after each function call.
Use `errors.New`, `fmt.Errorf`, `errors.Is`, and `errors.As` to handle errors.
Concurrency
Q: What is a goroutine, and how is it different from a thread?
A: A goroutine is a lightweight thread managed by Go runtime.
It consumes less memory and is scheduled efficiently by Go.
Q: What are buffered vs. unbuffered channels?
A: Unbuffered: sender and receiver wait for each other.
Buffered: can send/receive without waiting until full/empty.
Q: Explain select in Go. How is it useful in concurrency?
A: `select` waits on multiple channel operations.
Used for multiplexing, timeouts, and non-blocking communication.
Q: How do you avoid race conditions in Go?
A: Use `sync.Mutex`, channels, or `sync/atomic`.
Run code with `-race` flag to detect race conditions.
Q: What does sync.WaitGroup do, and how is it used?
A: It waits for a group of goroutines to finish using `Add()`, `Done()`, and `Wait()`.
Interfaces and Structs
Q: What is the difference between a value receiver and a pointer receiver?
A: Value receivers operate on a copy of the object.
Pointer receivers allow modifications to the original object and avoid copying large structs.
Q: How does Go implement polymorphism using interfaces?
A: Any type that implements all methods of an interface implicitly satisfies that interface,
enabling polymorphism without explicit declarations.
Q: What is type assertion? Give an example.
A: Used to extract the concrete type from an interface:
var i interface{} = "hello"
s := i.(string)
Q: Can a struct implement multiple interfaces in Go?
A: Yes. A struct can satisfy multiple interfaces by implementing their methods.
Advanced Topics
Q: How does Go handle panic and recover? When should you use them?
A: `panic` stops normal execution; `recover` can catch it inside a deferred function.
Used for handling unexpected errors like out-of-bounds or nil dereference.
Q: Explain Go's package system and how you manage module dependencies.
A: Go uses modules (`go.mod`, `go.sum`) to manage dependencies.
Packages are imported using paths and grouped logically in files.
Q: What is the use of init() function in a Go package?
A: `init()` runs automatically before `main()` or any other function.
Used for initialization logic.
Q: How does Go achieve abstraction without classes or inheritance?
A: Go uses interfaces and composition instead of inheritance for abstraction.
Q: Explain how you would handle versioning in Go modules.
A: Use semantic import versioning in `go.mod`.
e.g., `require example.com/pkg v2.0.1`
Testing and Tooling
Q: How do you write unit tests in Go? What is the role of go test?
A: Write test functions in *_test.go files using `testing` package.
Run with `go test`.
Q: What are table-driven tests? Give an example.
A: Use a slice of test cases and loop through them.
Example:
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {...})
Q: How do you mock dependencies in Go for testing purposes?
A: Use interfaces to abstract dependencies and pass mock implementations in tests.
Q: Explain the use of go vet, golint, and go fmt.
A: `go vet`: reports suspicious code.
`golint`: suggests style improvements.
`go fmt`: auto-formats Go code.
Performance & Optimization
Q: How do you profile and benchmark Go applications?
A: Use `pprof` for profiling CPU/memory.
Use `go test -bench` for benchmarking.
Q: What are some common performance bottlenecks in Go applications?
A: Excessive GC, unnecessary allocations, contention on locks or channels.
Q: How would you reduce GC overhead in a Go application?
A: Reuse memory with sync.Pool, avoid allocations in hot paths,
and reduce pointer-heavy data structures.
Scenario-Based / System Design
Q: How would you design a RESTful API in Go? What packages would you use?
A: Use `net/http` or frameworks like `Gin`, `Echo`. Define routes, handlers, and use JSON for
communication.
Q: Design a rate limiter using goroutines and channels.
A: Use a `time.Ticker` and buffered channel to allow a fixed number of requests per interval.
Q: How would you handle large file uploads in Go?
A: Stream file content using `multipart` without reading the whole file into memory.
Q: Explain how you would implement a background job queue in Go.
A: Use goroutines + buffered channel as job queue.
Use `sync.WaitGroup` to manage job execution.