How to Pass a Go Interview: Top Questions and Answers

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How to Pass a Go Interview: Top Questions



Go (Golang) is one of the most in-demand languages for building high-load server applications and microservices. An interview for a Go developer position often tests not only knowledge of syntax but also understanding of internal mechanisms: goroutines, channels, interfaces, and memory management. In this article, we will break down the top 10 questions most frequently asked in interviews, with code examples and explanations.



1. What is the difference between an array and a slice?



This is a classic question for understanding Go basics.



  • Array — is a fixed-length sequence of elements of the same type. The size of an array is part of its type.
  • Slice — is a dynamic structure that references an underlying array. A slice can grow and change its size.


// Array — fixed sizevar arr [3]int = [3]int{1, 2, 3}// Slice — dynamicvar slc []int = []int{1, 2, 3}

// Important: arr and [4]int are different types// Slices are passed by reference (pointer to underlying array)


Tip: If you need to change the length — use a slice. If you need a guarantee of a fixed size — use an array.



2. What are goroutines and how do they work?



A goroutine is a lightweight thread of execution managed by the Go runtime. Unlike OS threads, goroutines have a smaller stack (starting at 2 KB) and switch quickly.



  • Starting a goroutine: go func()
  • Goroutines operate in the same address space, so access to shared data must be synchronized (via channels or mutexes).


package main

import ( "fmt" "time")

func printNumbers() { for i := 1; i <= 5; i++ { time.Sleep(100 * time.Millisecond) fmt.Println(i) }}

func main() { go printNumbers() // starting a goroutine time.Sleep(1 * time.Second) // wait for completion fmt.Println("Main done")}


Important: The main function does not wait for goroutines to finish. For synchronization, use sync.WaitGroup or channels.



3. How do channels work?



Channels are typed "pipes" through which goroutines exchange data. They provide safe data transfer without explicit locks.



  • Unbuffered channel: The sender blocks until the receiver reads the data.
  • Buffered channel: The sender blocks only when the buffer is full.


ch := make(chan int)    // unbufferedchBuf := make(chan int, 3) // buffered (capacity 3)

// Sendinggo func() { ch <- 42}()

// Receivingvalue := <-chfmt.Println(value)

// Closing a channelclose(ch)// Reading from a closed channel returns the zero value


Common question: What happens if you read from a nil channel? — Answer: The goroutine will block forever (deadlock).



4. What are interfaces in Go and how are they different from other languages?



An interface in Go is a set of methods. Unlike Java or C#, interface implementation in Go is implicit (duck typing): a type automatically satisfies an interface if it implements all its methods.



type Speaker interface {    Speak() string}

type Dog struct{}func (d Dog) Speak() string { return "Woof!" }

type Cat struct{}func (c Cat) Speak() string { return "Meow!" }

func makeSound(s Speaker) { fmt.Println(s.Speak())}

func main() { makeSound(Dog{}) // Woof! makeSound(Cat{}) // Meow!}


Why this is important: It allows writing flexible, loosely coupled code. The empty interface interface{} (or any in Go 1.18+) can hold a value of any

Blogs

Book Recommendations