Top Go Interview Questions: defer, GMP Scheduler, Slice Internals, Channels, and Context
This article presents a curated collection of 30 essential Go interview questions covering language characteristics, object‑orientation, inheritance, polymorphism, goroutine basics, channel usage, defer execution order, memory allocation, slice vs array, map safety, error handling, context, Go modules, the GMP scheduler, and practical code examples for testing and middleware.
1. What are the characteristics of Go?
Go features simple syntax, fast compilation, easy deployment, strong concurrency, a rich standard library, and unified engineering standards, making it suitable for backend services, micro‑services, cloud‑native applications, and command‑line tools.
2. Is Go an object‑oriented language?
Go is not a traditional object‑oriented language; it lacks class, extends, and implements, but supports structs, methods, interfaces, and composition to achieve encapsulation, polymorphism, and code reuse.
3. How does Go implement inheritance?
Go does not support classic inheritance; reuse is typically achieved through struct composition.
type Animal struct { Name string }
type Dog struct { Animal }4. How does Go achieve polymorphism?
Polymorphism is realized via interfaces: any type that implements all methods of an interface automatically satisfies that interface.
5. What is a goroutine?
A goroutine is Go’s lightweight concurrent execution unit, started with the go keyword.
go task()6. Difference between goroutine and OS thread
Goroutines are scheduled by the Go runtime and are much lighter than OS threads, which are scheduled by the operating system. A program can create many goroutines but cannot arbitrarily create many OS threads.
7. What is a channel?
A channel is a pipe used for communication between goroutines.
ch := make(chan int)
ch <- 1
value := <-ch8. Unbuffered vs buffered channels
Unbuffered channels block the sender until a receiver is ready; buffered channels allow sending until the buffer is full and receiving until the buffer is empty.
9. What is select used for?
selectmonitors multiple channels and executes the case whose channel is ready, commonly used for timeout control, multiplexing, and task cancellation.
10. Execution order of defer
Multiple defer statements are executed in last‑in‑first‑out order, similar to a stack.
11. Difference between new and make
newallocates memory and returns a pointer; make initializes slices, maps, and channels and returns the value itself.
12. Slice vs array
Arrays have fixed length and are value types; slices have variable length and are references to an underlying array.
13. Is map concurrent‑safe?
A plain map is not safe for concurrent reads and writes. When accessed by multiple goroutines, use sync.Mutex, sync.RWMutex, or sync.Map.
14. What is an interface?
An interface is a set of methods; any type that implements all methods implicitly satisfies the interface.
15. Use of the empty interface
The empty interface can hold values of any type. Since Go 1.18, the alias any is commonly used.
16. How does Go handle errors?
Errors are typically handled by returning an error value.
result, err := doSomething()
if err != nil {
return err
}17. Difference between panic and error
errorrepresents expected business errors; panic signals unrecoverable severe errors and should not be used frequently in business code.
18. What is recover for?
recovercaptures a panic to prevent the program from crashing, usually used together with defer.
19. Purpose of context
contextcarries timeout, cancellation, and small request‑level data across API boundaries.
20. What is Go Modules?
Go Modules is the official dependency management system, using go.mod and go.sum to track module versions.
21. What does go mod tidy do?
go mod tidyremoves unused dependencies, adds missing ones, and updates go.sum.
22. What is the GMP model?
GMP is Go’s runtime scheduler that maps many goroutines onto a smaller set of OS threads.
23. When to choose value receiver vs pointer receiver?
Use a pointer receiver when the method needs to modify the struct’s fields or when the struct is large; for a uniform method set, prefer pointer receivers throughout.
24. How to achieve interface segregation in Go?
Define small interfaces that expose only the methods actually needed by callers, e.g., a Reader interface instead of a large composite interface.
25. When is the init function executed?
initruns automatically during package initialization: first all imported packages, then the current package, and finally main.
26. What is Go’s garbage collector?
Go’s GC automatically reclaims memory that is no longer reachable. It is suitable for server‑side workloads, though high‑performance scenarios should still minimize unnecessary allocations.
27. How to profile Go programs?
Use the pprof tool, e.g., via the runtime/pprof or net/http/pprof packages.
28. How to write unit tests in Go?
Test files end with _test.go and test functions start with Test.
func TestAdd(t *testing.T) {
got := Add(1, 2)
if got != 3 {
t.Fatal("result incorrect")
}
}Run tests with:
go test ./...29. What is the principle of Gin middleware?
Gin middleware is a gin.HandlerFunc that runs before or after the main handler, useful for authentication, logging, CORS, and panic recovery.
30. JWT login flow
User logs in.
Backend verifies credentials.
Backend generates a JWT.
Frontend stores the token.
Frontend includes the token in subsequent API requests.
Backend middleware parses the token.
On successful parsing the request proceeds; otherwise a 401 response is returned.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Golang Shines
We share daily the latest Golang technical articles, practical resources, language news, tutorials, and real-world projects to help everyone learn and improve.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
