Top 10 Technical Go Interview Questions
Tags: engineeringpractices, go, languages, career
Published: 2020-09-04 13:34:16

---
title: Top 10 Technical Go Interview Questions
published: true
date: 2020-08-31 13:20:23 UTC
tags: EngineeringPractices,Golang,Languages,career
canonical_url: https://qvault.io/2020/08/31/top-10-technical-go-interview-questions/
---

 ![Top 10 Go Interview Questions](https://qvault.io/wp-content/uploads/2020/08/Top-10-Go-Interview-300x169.png "Top 10 Go Interview")

The post [Top 10 Technical Go Interview Questions](https://qvault.io/2020/08/31/top-10-technical-go-interview-questions/) first appeared on [Qvault](https://qvault.io).

Are you interviewing for a new Go developer position? Perhaps you are about to interview someone yourself? Let’s review some good questions to be familiar with, whether you are the interviewer or the interviewee.

If you are interested in a more interactive way to review for a Go interview, try our [Interview Prep – Go](https://app.qvault.io/) course. You’ll find more questions and will be able to run code in the browser and get instant feedback on your level of preparedness.

[Take the Interview Prep Course Free](https://app.qvault.io)

## Questions and Answers

### 1. What is the difference between a goroutine and an operating system thread?

- Go provides built-in channels for goroutines to communicate safely between themselves.
- More goroutines can run on a typical system than you can threads. For example, with Java you can likely run thousands of threads. With Go, you can likely run millions of goroutines.
- Goroutines startup more quickly than operating system threads.
- Multiple Goroutines are multiplexed onto OS threads, rather than a 1:1 mapping.
- You can write massively concurrent servers without having to resort to event programming.
- Goroutines are not hardware dependent like threads.
- Goroutines are more lightweight, largely due to [segmented stacks](https://blog.cloudflare.com/how-stacks-are-handled-in-go/#segmentedstacks) in memory

### 2. Can constants be computed in Go?

Constants can not be computed at runtime, their value must be known at compile time. That said, constants can be computed at compiled time, typically as a derivative of other constants. For example:

```go
const hours = 7643

const minutes = hours * 60

```

### 3. What does the Go ecosystem use for package and dependency management?

Until recently the GOPATH setup allowed developers to import packages as long as they were in the local Go workspace. As of Go ~1.13 it is considered best practice to use the `go mod` setup where source code isn’t required to be a part of the GOPATH.

Go doesn’t use a package manager like NPM or Cargo. The Go toolchain provides commands like `go get` for fetching external dependencies straight from their Git source control repositories, and `go mod` for managing the dependencies of a specific project.

### 4. How would you succinctly swap the values of two variables in Go?

```go
var1, var2 = var2, var1
```

### 5. Do you have any preferences for error handling methodologies in Go?

Errors in Go are an interface type, where any type that implements the single `Error()` method can be considered an error:

```go
type error interface {
    Error() string
}
```

Whenever a function has a possibility to go wrong, like a network call or a type conversion, the function should return an error as its last return variable. The caller should check the error value, and any value other than `nil` is considered an error.

Idiomatic Go developers should prefer [guard clauses](https://qvault.io/2019/08/16/guard-clauses-how-to-clean-up-conditionals/) over if-else chains, especially when handling errors. Errors should also be [wrapped in a meaningful way](https://qvault.io/2020/03/09/wrapping-errors-in-go-how-to-handle-nested-errors/) as they are passed up the call stack if appropriate.

## 6. What is a pointer and when would you use it?

A pointer holds the memory address of a value.

- `&` generates a pointer to its operand.
- `*` dereferences a pointer (exposes the underlying value).

Pointers can be used to:

- Allow a function to directly mutate a value that is passed to it
- To increase performance in edge cases. Sometimes passing a large struct of data results in inefficient copying of data
- To signify the lack of a value. For example, when unmarshalling JSON data into a struct it can be useful to know if a key was absent rather than it being present with the zero value.

### 7. Describe the difference between [sync.Mutex](https://golang.org/pkg/sync/#Mutex) and [sync.RWMutex](https://golang.org/pkg/sync/#RWMutex)

The normal mutex locks data so that only one goroutine can access it at a time.

A RWMutex (read/write) can lock data for “reading” and for “writing”. When locked for reading, other readers can also lock and access data. When locked for writing no other goroutines, including other readers can access the data.

### 8. Consider the following code. What will be the value of s1?

```go
primes := [6]int{2, 3, 5, 7, 11, 13}
s1 := primes[1:4]
```

`s1` will be: `[3 5 7]`

When slicing an existing array or slice the first index is inclusive while the last index is exclusive. If an index is omitted on one side of the colon, then all values until the edge of the original slice are included in the result.

### 9. Are channels and maps safe for concurrent access?

Channels are safe for concurrent access, for this reason they have blocking operations. Maps are unsafe for concurrent access and require a locking mechanism like a mutex to be safely used across goroutines.

### 10. How would you sort a slice of custom structs?

I would build a new type that represents a slice of that struct type. For example:

```go
type fruitSlice[]fruit

type car struct {
	size int
	color string
}
```

Then I would fulfill the standard library’s [sort.Interface](https://golang.org/pkg/sort/#Interface):

```go
type Interface interface {
    Len() int
    Less(i, j int) bool
    Swap(i, j int)
}
```

I would then be able to use the [sort.Sort](https://golang.org/pkg/sort/#Sort) function:

```go
sort.Sort(fruitSlice(cars))
```

## Thanks For Reading!

Follow us on Twitter [@q\_vault](https://twitter.com/q_vault) if you have any questions or comments

Take game-like coding courses on [Qvault Classroom](https://classroom.qvault.io/#/)

[Subscribe](https://mailchi.mp/5c7f5c281bbe/qvault-newsletter-subscribe) to our Newsletter for more educational articles