Go in an Interview: 9 Pitfalls and How to Avoid Them

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

Introduction: Why Go and Why Is It So Hard?

\n\n

Go (or Golang) is a language that people love for its simplicity and hate for the same simplicity. In Go interviews, you are rarely asked to "write a bubble sort" (although that happens too). More often, you will face tasks on concurrency, understanding memory management, and the ability to write clean, idiomatic code. If you are used to Python or Java, Go may seem too strict. But it is this very strictness that makes it an ideal language for production systems.

\n\n

In this article, we will break down the key topics that will definitely come up in an interview. You will get not just theory, but specific code examples and an explanation of what's "under the hood." Let's go!

\n\n

1. Goroutines and Channels: The Basics You Need to Know by Heart

\n\n

Goroutines are not Threads

\n

The most common question: "How is a goroutine different from an OS thread?" Answer: a goroutine is lightweight (stack from 2 KB), managed by the Go scheduler, not the kernel. A million goroutines is normal. A million threads will kill the system.

\n\n
// Example: launching 10 goroutines\nfor i := 0; i < 10; i++ {\n    go func(id int) {\n        fmt.Printf("Goroutine %d is working\\n", id)\n    }(i)\n}\n// Wait (bad way, just for example)\ntime.Sleep(time.Second)\n
\n\n

Channels: Synchronization Without Pain

\n

Channels in Go are typed "pipes" for transferring data between goroutines. Remember three rules:

\n
    \n
  • Writing to a closed channel causes a panic.
  • \n
  • Reading from a closed channel returns the zero value.
  • \n
  • An unbuffered channel blocks the goroutine until a receiver appears.
  • \n
\n\n
// Correct work with a channel\nch := make(chan int, 2) // buffered\n\nch <- 1\nch <- 2\nclose(ch)\n\nfor val := range ch {\n    fmt.Println(val) // 1, 2\n}\n
\n\n

Interview Tip: if asked about 'select', show an example with a timeout:

\n\n
select {\ncase msg := <-ch:\n    fmt.Println(msg)\ncase <-time.After(1 * time.Second):\n    fmt.Println("Timeout!")\n}\n
\n\n

2. Interfaces: Not Just Methods, But Contracts

\n\n

In Go, interfaces are implemented implicitly. This means a struct automatically satisfies an interface if it has all the required methods. A common question is: "What is the difference between interface{} and any?" — answer: nothing, they are synonyms (since Go 1.18).

\n\n

Typical Interview Task

\n

Write a function that takes an io.Writer and writes a string to it. This tests understanding of interfaces from the standard library.

\n\n
func WriteHello(w io.Writer) {\n    _, err := w.Write([]byte("Hello, Go!"))\n    if err != nil {\n        log.Fatal(err)\n    }\n}\n\n// Usage:\nvar buf bytes.Buffer\nWriteHello(&buf)\nfmt.Println(buf.String()) // Hello, Go!\n
\n\n

Pitfall: all interfaces in Go are two pointers (type + value). An empty interface (interface{}) is not equal to nil if it holds (*int)(nil). Remember this to avoid failing the question about nil != nil.

\n\n

3. Generics: New, But Already Mandatory

\n\n

Since Go 1.18, generics have become part of the language. In an interview, you may be asked to write a generic function. For example, a function that takes a slice of any type and returns its length (trivial) or finds the maximum.

\n\n
// Generic function to find the maximum\nfunc Max[T constraints.Ordered](a, b T) T {\n    if a > b {\n        return a\n    }\n    return b\n}\n\n// Usage:\nfmt.Println(Max(10, 20))     // 20\nfmt.Println(Max(3.14, 2.71)) // 3.14\n
\n\n

Note the constraints package — it is often used in questions. But remember: since Go 1.21, it has been replaced by the built-in cmp.Ordered.

\n\n

4. Error Handling: Don't Ignore Them

\n\n

Go has no try-catch. Errors are values.

Blogs

Book Recommendations