Go (Golang) Interview Questions and Answers
Go interviews test whether you think in Go: small interfaces, composition over inheritance, explicit error values and zero values that are useful. Expect questions on slices versus arrays, maps, pointers, defer, panic and recover, and how method sets and interfaces interact.
Concurrency is the centre of gravity: goroutines and the scheduler, channels (buffered and unbuffered), select, sync primitives, the context package for cancellation and deadlines, data races and how to find them. Senior rounds add profiling, memory and garbage collection, and structuring services.
Questions
- What is the Go programming language and who created it?
- What are the ways to declare variables in Go?
- What does 'zero value' mean and what are the zero values for common types?
- When can you use := and when must you use var?
- How do constants work and what is iota?
- What is a slice and how does it differ from an array?
- What happens when you append to a slice?
- How do you use maps in Go?
- What is a struct and how do you define one?
- How does Go handle multiple return values?
- How do pointers work and does Go have pointer arithmetic?
- Why can := accidentally shadow a variable?
- What loop forms does Go provide?
- How does ranging over a string behave?
- What is the difference between a byte and a rune?
- What is the idiomatic way to handle errors?
- How do packages and imports work?
- What does the defer statement do?
- What is a goroutine and how do you start one?
- What are channels used for?
- How does fmt formatting work?
- What does nil represent?
- How does type conversion work in Go?
- What is _ used for?
- How do variadic parameters work?
- Which types are comparable with == in Go?
- What is special about Go's switch?
- How do closures capture variables in Go?
- What do len and cap return for a slice?
- Why does Go enforce a single formatting style?
- Why should you use the fmt package instead of built-in print?
- What are the ways to compare strings and which is fastest?
- What are the main strings package functions for splitting and joining?
- What are the core types in the time package?
- What are the common file I/O patterns?
- What does the flag package do and how do you read environment variables?
- What does the strconv package provide?
- Write a function that reverses a string correctly for Unicode.
- Write an efficient palindrome checker.
- Write a function that returns duplicate elements.
- What is the difference between buffered and unbuffered channels?
- How is select used with channels?
- How do interfaces work and how are they satisfied?
- What is interface{} / any used for?
- How do type assertions and type switches work?
- When should a method use a pointer receiver vs a value receiver?
- How does struct/interface embedding provide composition?
- What is sync.WaitGroup used for?
- How do sync.Mutex and sync.RWMutex protect shared state?
- When and how should you close a channel?
- How do you wrap and inspect errors?
- How do you create a custom error type?
- What causes goroutine leaks and how do you prevent them?
- What is the context package used for?
- How does the testing package work?
- How does dependency management with modules work?
- Why can an interface holding a nil pointer be non-nil?
- Can a deferred function modify named return values?
- How do you build multidimensional slices?
- What are the ways to copy slice data and their pitfalls?
- When do you use make vs new?
- How do type parameters work since Go 1.18?
- What is go vet and how is it different from the compiler?
- How does encoding/json marshal and unmarshal work?
- What is the difference between a method value and a method expression?
- What is the fmt.Stringer interface?
- How and when does init() run?
- What is the difference between time.Timer and time.Ticker?
- Why can defer inside a loop be a problem?
- How do bufio.Scanner and bufio.Reader help with I/O?
- How do you combine multiple errors in Go 1.20+?
- Why is strings.Builder faster than string concatenation?
- How does time formatting with 2006-01-02 work?
- What are the sorting options and how do they differ?
- Why are io.Reader and io.Writer so central to Go?
- How do you stream data versus buffering it all?
- What is special about Go's regexp package?
- When do you use bytes.Buffer versus strings.Builder?
- What is the difference between math/rand and crypto/rand?
- How should you do logging in a modern Go service?
Related
- Java interview questions
- Microservices interview questions
- System design interview questions
- Practice coding problems online
Frequently asked questions
What is a goroutine?
A lightweight function execution managed by the Go runtime scheduler, not by the operating system. Thousands can run on a few OS threads because they start small and are multiplexed.
Buffered versus unbuffered channels?
An unbuffered channel makes sender and receiver meet (synchronise); a buffered channel lets senders continue until the buffer is full.
How do you avoid data races in Go?
Share memory by communicating over channels, protect shared state with sync.Mutex or atomic operations, and run tests with the race detector (go test -race).