Go Interview Preparation
# CHAPTER 28
Go Interview Preparation
1. Introduction
Congratulations on completing the technical coursework! You now understand Go from basic syntax to advanced concurrency and REST APIs. This chapter consolidates that knowledge into high-yield interview questions designed to prepare you for junior to mid-level Golang backend developer interviews.2. Learning Objectives
By the end of this chapter, you will be able to:- Answer the most frequent Core Go theoretical questions.
- Explain Concurrency, Goroutines, and Channels confidently.
- Discuss memory management (Pointers and Slices).
- Understand common Go white-board coding challenges.
3. Core Go Interview Questions
1. What makes Go different from languages like Java or C++?
*Answer:* Go was designed for simplicity, fast compile times, and modern cloud architecture. It removes complex features like Classes, Inheritance, and Exceptions (try/catch). Instead, it uses Structs, Interfaces (implicit implementation), explicit error returning (if err != nil), and has built-in lightweight concurrency (Goroutines).
2. Explain the difference between an Array and a Slice.
*Answer:* An Array has a fixed size defined at compile-time (e.g., [5]int), meaning it cannot grow or shrink. A Slice (e.g., []int) is a dynamic, flexible wrapper over a hidden underlying array. Slices can grow using the append() function.
3. What is the difference between len() and cap() on a Slice?
*Answer:* len() returns the number of elements currently accessible in the slice. cap() returns the total capacity of the hidden underlying array before Go has to allocate a brand new, larger array in memory to accommodate more append calls.
4. What is the defer keyword and when is it used?
*Answer:* defer schedules a function call to be executed at the very end of the surrounding function, right before it returns. It is most commonly used to ensure resources are cleaned up, such as defer file.Close() or defer db.Close(), even if a panic occurs earlier in the function.
4. OOP and Memory Interview Questions
5. How does Go achieve Object-Oriented Programming without Classes? *Answer:* Go uses Structs to encapsulate state (data fields). It attaches behaviors to these structs using Receiver Functions (Methods). Instead of Inheritance, Go uses Composition (embedding structs inside other structs). Finally, Go achieves Polymorphism through implicitly implemented Interfaces.
6. Explain the difference between a Value Receiver and a Pointer Receiver.
*Answer:* A Value Receiver (func (u User)) gets a copy of the struct; modifications do not affect the original object. A Pointer Receiver (func (u *User)) receives the memory address; modifications directly alter the original struct. Pointer receivers also save memory by preventing the copying of large structs.
7. When should you use Pointers in Go? *Answer:* Use pointers when you need a function to mutate (modify) the original variable, or when passing massive structs to avoid the CPU overhead of copying large blocks of memory. You generally do *not* need pointers for slices or maps, as they are reference types natively.
5. Concurrency Interview Questions
8. What is a Goroutine, and how is it different from an OS Thread? *Answer:* A Goroutine is a virtual, lightweight thread managed by the Go Runtime Scheduler, not the Operating System. While an OS thread uses ~1MB of RAM, a Goroutine uses only ~2KB. This allows Go to run hundreds of thousands of concurrent tasks simultaneously on minimal hardware.
9. What is a Channel, and what is the difference between Buffered and Unbuffered? *Answer:* A Channel is a synchronized pipe used to safely pass data between Goroutines. An Unbuffered channel (capacity 0) blocks the sender until a receiver is ready, ensuring exact synchronization. A Buffered channel (e.g., capacity 5) allows the sender to drop off data and continue executing immediately, as long as the queue isn't full.
10. How do you prevent Race Conditions in Go?
*Answer:* The preferred Go way is to use Channels so that only one Goroutine "owns" the data at a time. If shared memory is required, use a sync.Mutex to lock and unlock the variable block. You should always test your code using the go run -race flag.
6. Common Coding Challenges
Challenge 1: String Reversal (Dealing with Runes!)
*Warning:* If you just swap bytes, you will corrupt emojis/UTF-8 characters! You must convert to a slice of rune first.
Challenge 2: Concurrent API Fetching
*Task:* Spawn 3 Goroutines, and wait for all to finish before exiting main.
*Solution Idea:* Use sync.WaitGroup. Call wg.Add(1) before spawning, pass &wg into the goroutine, call defer wg.Done() inside the goroutine, and wg.Wait() in main.
7. Common Mistakes to Avoid in Interviews
-
Saying Go has "Exceptions": Never say this. Go uses
errorreturn values.
-
Forgetting pointer syntax: If asked to write a function that modifies a variable, do not forget the
*in the parameter and the&in the argument.
- Overcomplicating Concurrency: If asked to synchronize two tasks, don't write complex Mutex locks if a simple Channel will solve it. "Share memory by communicating."
8. Exercises
- 1. Hand-write the answers to the 10 theoretical questions above.
-
2.
Open an IDE and write a Go program that implements the FizzBuzz challenge using a
forloop andif/else ifstatements.
9. MCQs with Answers & Explanations
Which of the following is true about Go's OOP capabilities?
What is the primary benefit of Goroutines?
If a method needs to change the data inside the struct it is attached to, what must be used?
What is the Go philosophy for error handling?
When an unbuffered channel is used, what happens to the sending Goroutine?
What built-in Go tool is used to detect memory corruption caused by concurrent writes?
What does the defer keyword do?
text[0])?
a) Because bytes are slow b) Because UTF-8 characters (like emojis) can span multiple bytes, and swapping raw bytes will corrupt them. You must convert to []rune.
Answer: b) Because UTF-8 characters can span multiple bytes.
Which Go package handles JSON encoding and decoding?
How does Go format code to standard industry guidelines automatically?
10. Interview Questions
-
Q: Explain the "Comma Ok" idiom when looking up values in a Map. (Answer: Maps return Zero Values for missing keys.
val, ok := map[key]uses the booleanokto safely verify if the key actually exists).
-
Q: How does a Go web server handle thousands of simultaneous HTTP requests? (Answer: The
net/httppackage automatically spawns a new Goroutine for every incoming request).