1. What is Go and why is it popular?
Go is an open-source programming language developed by Google. It is designed for simplicity, concurrency, fast compilation, and scalability.
Key advantages:
- Simple syntax
- Fast execution
- Built-in concurrency (goroutines)
- Garbage collection
- Strong standard library
- Great for microservices and cloud systems
2. What are goroutines?
Goroutines are lightweight threads managed by the Go runtime.
Example:
go myFunction()
Features:
- Very low memory usage
- Faster than OS threads
- Used for concurrent programming
3. What is a channel in Go?
Channels are used for communication between goroutines.
Example:
ch := make(chan int)
go func() {
ch <- 10
}()
value := <-ch
fmt.Println(value)
Benefits:
- Safe communication
- Synchronization between goroutines
- Avoids shared memory issues
4. Difference between buffered and unbuffered channels?
Unbuffered Channel
- Sender waits until receiver receives data.
ch := make(chan int)
Buffered Channel
- Sender can send until buffer is full.
ch := make(chan int, 3)
5. What is the difference between concurrency and parallelism?
Concurrency
Handling multiple tasks at the same time.
Parallelism
Executing multiple tasks simultaneously using multiple CPUs.
Go supports both using goroutines.
6. What is GOMAXPROCS?
GOMAXPROCS sets the maximum number of OS threads executing Go code simultaneously.
Example:
runtime.GOMAXPROCS(4)
7. What are pointers in Go?
Pointers store memory addresses.
Example:
x := 10
p := &x
fmt.Println(*p)
Notes:
- Go supports pointers
- No pointer arithmetic
8. What is the difference between new() and make()?
new()
Allocates memory and returns pointer.
p := new(int)
make()
Initializes slices, maps, and channels.
m := make(map[string]int)
9. What are slices in Go?
Slices are dynamic views over arrays.
Example:
nums := []int{1,2,3}
Features:
- Dynamic size
- Backed by arrays
- Contains length and capacity
10. Difference between array and slice?
| Array | Slice |
|---|---|
| Fixed size | Dynamic size |
| Value type | Reference-like |
| Size part of type | Flexible |
11. What is the difference between len and cap?
len
Current number of elements.
cap
Maximum capacity before reallocation.
Example:
s := make([]int, 3, 5)
fmt.Println(len(s)) // 3
fmt.Println(cap(s)) // 5
12. What are interfaces in Go?
Interfaces define behavior.
Example:
type Animal interface {
Speak()
}
Important:
- Implicit implementation
- No
implementskeyword
13. What is an empty interface?
interface{}
It can hold values of any type.
Equivalent to:
any
14. What is type assertion?
Used to extract concrete value from interface.
Example:
var i interface{} = "hello"
s := i.(string)
Safe assertion:
s, ok := i.(string)
15. What is a struct?
Structs group related data fields.
Example:
type User struct {
Name string
Age int
}
16. Does Go support inheritance?
Go does not support classical inheritance.
Instead, it uses:
- Composition
- Embedding
Example:
type Engine struct {}
type Car struct {
Engine
}
17. What is embedding in Go?
Embedding allows one struct to include another struct directly.
It promotes composition over inheritance.
18. What is defer?
defer delays execution until surrounding function returns.
Example:
defer fmt.Println("done")
Common uses:
- Closing files
- Unlocking mutexes
- Cleanup tasks
19. What is panic and recover?
panic
Stops normal execution.
panic("error")
recover
Catches panic inside deferred functions.
defer func() {
recover()
}()
20. What is garbage collection in Go?
Go automatically frees unused memory using garbage collection.
Benefits:
- Reduces memory leaks
- Easier memory management
21. What is a mutex?
Mutex prevents race conditions.
Example:
var mu sync.Mutex
mu.Lock()
counter++
mu.Unlock()
22. What is a race condition?
When multiple goroutines access shared data simultaneously causing unexpected behavior.
Go provides race detector:
go run -race main.go
23. What is context package used for?
The context package is used for:
- Cancellation
- Timeouts
- Passing request-scoped values
Example:
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
24. What is select statement?
select works with multiple channels.
Example:
select {
case msg := <-ch1:
fmt.Println(msg)
case <-time.After(time.Second):
fmt.Println("timeout")
}
25. What are packages in Go?
Packages organize reusable code.
Every Go file starts with:
package main
26. Difference between process and thread?
| Process | Thread |
|---|---|
| Independent execution unit | Lightweight execution path |
| More memory | Less memory |
| Slower context switch | Faster context switch |
Goroutines are lighter than threads.
27. What are maps in Go?
Maps store key-value pairs.
Example:
m := map[string]int{
"a": 1,
}
Check existence:
v, ok := m["a"]
28. Can Go functions return multiple values?
Yes.
Example:
func divide(a, b int) (int, error)
29. What are variadic functions?
Functions accepting variable arguments.
Example:
func sum(nums ...int)
30. What is dependency management in Go?
Go uses modules.
Initialize:
go mod init app
Install package:
go get package-name
Advanced Golang Interview Questions
31. Explain scheduler in Go.
Go scheduler manages goroutines using:
- G → Goroutine
- M → OS Thread
- P → Processor
Known as GMP model.
32. What is channel deadlock?
Occurs when goroutines wait forever.
Example:
ch := make(chan int)
ch <- 1 // deadlock
33. What is sync.WaitGroup?
Waits for goroutines to finish.
Example:
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
}()
wg.Wait()
34. What are worker pools in Go?
Pattern for limiting concurrent goroutines using channels and workers.
Common in:
- APIs
- Job processing
- Background tasks
35. What is the difference between nil slice and empty slice?
Nil slice
var s []int
Empty slice
s := []int{}
Both have length 0, but nil slice equals nil.
36. What are common Go design principles?
- Simplicity
- Composition over inheritance
- Explicit error handling
- Concurrency-first design