get in touch

Golang Interview Questions (and Answers) to Assess Candidates' Skills

author image
Yuliia Podorozhko
Market Researcher

Golang is an open-source programming language that’s similar to C but is optimised for quick compilation, seamless concurrency, and ease of use. Since its release in late 2000’s, Golang has only grown in popularity and companies such as Google, Twitch, Dropbox and SoundCloud all use this programming language.

MadAppGang has used Golang for more than a decade on projects that require high performance, reliability, and quick implementation. Our latest project, Evergen, is built with Go. As it was a large project, we had to expand our team and bring in some new Go experts. 

As we’ve hired Go experts both in the past and recently, we decided to share our knowledge with anyone looking for a Go developer. In this post we’ve compiled our favourite Golang interview questions. They'll help you hire a top specialist and make preparing for the interview fast and easy. Let’s take a look. 

Golang interview: 10 questions and answers

Below are ten essential questions (and the answers) you can use when interviewing candidates for a middle-level to senior Go developer position. Note that some of these tasks have more than one possible solution, but we’ve done our best to give the most predictable responses so you can get an idea of the candidate's expertise.

It's better if another developer or a development team leads interviews. Interviewing a developer is a challenging task, especially for those who have no knowledge in the field. However, if you must do so, these questions are helpful.

 

1. What is a goroutine? How can you stop your goroutine?

Goroutines are actions (functions or methods) that can be executed concurrently. A program always contains at least one main Goroutine. All goroutines work under the main goroutine, so if the main goroutine is terminated, all goroutines within the program will also be terminated. 

Goroutines work in backgrounds. To stop a goroutine, it’s necessary to pass a signal channel to it. The channel will send a value when you want the function to finish.

blog image

2. What are channels and how can you use them in Golang?

Channels are a Golang concept for communicating between different goroutines and work much like pipes connecting different concurrent goroutines. By default, communications are bidirectional. In this way, goroutines can synchronise without explicit locks or condition variables.

go language interview question channelss

Sending data from one Goroutine to another. Source: Educative

3. How to convert a []T to an []interface{}?

There is no way to convert a []T to an []interface{} directly. Golang’s language specifications do not allow this conversion because the types do not have the same memory representation. Instead, the elements must be copied individually to the destination slice. The following example shows how to convert a slice of []T to an []interface{}:

t := []int{1, 2, 3, 4}

s := make([]interface{}, len(t))

for i, v := range t {

    

    s[i] = v

}

4. What would you use for an empty struct{}?

An empty struct (struct{}) is a struct type with no fields. The thing about struct{} is that it occupies no storage space. When a property of a data structure is more important than its value, it is better to use an empty struct to save memory. 

a := struct{}{}

println(unsafe.Sizeof(a))

// Output: 0

In most cases, the storage savings are insignificant and are related to the size of a slice or map. In fact, the primary purpose of an empty struct is to demonstrate to readers that there is no need for a value. So, its role is informative.

5. Have you worked with Go 2?

This is a trick question as, at the time of writing, no one has worked with Go 2 yet. The latest version of Go 1.17 was released on August 16, 2021, and it didn't introduce any language changes. The last language change was presented in version 1.14. 

blog image

Source: Reddit

However, there are many rumours about Go 2 appearing soon. Here are some of the possible features:

  • Provide backwards compatibility by fixing the most significant problems with scaling in Go.
  • Go 2 shouldn't split the ecosystem of Go.

6. What are the differences between unbuffered and buffered channels?

A channel is a mechanism that allows synchronous data exchange between goroutines. Based on how they exchange data, there are two types of channel: 

  • Unbuffered channel – There is no storage capacity on the inside of such a channel at first. It’s necessary to fill the message in an unbuffered channel to enable the goroutine process. 

  • Buffered channel – A buffered channel, unlike an unbuffered one, is capable of storing messages. Moreover, a buffered channel can contain several messages up to its defined capacity.

7. Why does Go compile faster than Java or C/C++?

Go compiles faster than other programming languages such as C/C++, Java, and Rust due to the following reasons:

  • In Golang, all imports are listed at the beginning of every source file. Thus, to determine file dependencies, the compiler doesn't have to read and process an entire file. 

  • Golang package dependencies form a directed acyclic graph, so there are no cycles. This means that packages can be compiled separately and concurrently. 

  • Since Go imports dependencies once for all files, the import time does not increase exponentially with project size. For each import in a package, the compiler must read an object file, but does not need to look further than the files in question.

8. When does the Go runtime allocate memory from the heap, and when from the stack?

Go manages memory allocation automatically. This prevents a whole class of potential bugs, but it doesn’t completely free the programmer from considering the mechanics of allocation. Go allocates memory in two places: a global heap for dynamic allocations and a local stack for each goroutine.

blog image

Source: Medium

In a typical Go program, allocations occur primarily on the stack. Sometimes, however, the compiler follows rigid rules and allocates memory from the heap. For example, "new", a built-in function, is always contributed to the heap. Also, in some cases, the compiler uses the "escape analysis" to decide whether the object can stay on the stack or should go to the heap.

9. The app you use as a rest API for third parties is running in your K8 cluster. Suddenly, it's unavailable (API request timeout error). What are the possible ways to detect the cause of the problem?

Here are three possible ways to detect what has gone wrong: 

1. Check the quantity of live/ready pods for the app in discussion.

2. Check if there are service routes and ingress routes for the app in use.

3. Check the metrics and general conditions of your ingress.

10. When testing the newest version of your app, you notice that the read-only request for "the faster motorbike" from the MongoDB collection is slower than required by your SLO (SLO:10 ms, SLI:10 sec). What are the possible ways to resolve the problem?

Here is a good example of a problem-solving scenario:

1. Check the metrics of the MongoDB cluster. The problem is probably in the overloaded primary node. If that’s the case, it’s necessary to find what overloaded the node in discussion. 

2. Check the query plan and according to the situation:

  • Optimise the query
  • Add indexes to the corresponding fields used for sorting

3. Change the data storage mode. It might be necessary to conduct “sharding.”

4. Add read-cache.

Final word

By using these Golang interview questions, you'll be able to evaluate the qualifications and experience of interview candidates. However, don't forget that, even for a small project, building an in-house team is a long and difficult process. And when it comes to Golang, you risk project failure even at the recruitment stage. The demand for developers is soaring worldwide, and it can take months to find qualified and suitable Go developers and build a team.

But there’s no need to give up on your plans because you can hire a dedicated software development company, like us here at MadAppGang. We have a strong team of Golang developers who are ready to bring your idea to life. 

Plus, because we work on various projects, we also have Python and Java specialists on hand. If it's necessary for your project, or you decide to use Go for backend, and React for frontend, for instance, we can make it happen. 

Contact us and let’s take your project from conception to realisation.