Top 10 Go Interview Questions

Top 10 Technical Go Interview Questions

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 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.

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 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:

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?

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:

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 over if-else chains, especially when handling errors. Errors should also be wrapped in a meaningful way 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 and 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?

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:

type fruitSlice[]fruit

type car struct {
	size int
	color string
}

Then I would fulfill the standard library’s sort.Interface:

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

I would then be able to use the sort.Sort function:

sort.Sort(fruitSlice(cars))

Thanks For Reading!

Follow us on Twitter @q_vault if you have any questions or comments

Take some coding courses on our new platform

Subscribe to our Newsletter for more programming articles

%d bloggers like this: