Learn coding concepts by building real projects with modern technologies

Top 15 Golang Interview Questions [Updated 2022]

By Lane Wagner on Aug 31, 2020

Let’s take a look at some good technical questions to be familiar with, whether you are the interviewer or the interviewee.

1. What’s the difference between a goroutine and an operating system thread?

Learn Go by writing Go code

I'm a senior engineer learning Go, and the pace of Boot.dev's Go Mastery courses has been perfect for me. The diverse community in Discord makes the weekly workshops a blast, and other members are quick to help out with detailed answers and explanations.

- Daniel Gerep from Cassia, Brasil

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 compile-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’s recommended to use go mod. With go mod, source code isn’t required to be a part of the GOPATH environment variable.

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

Learn JavaScript by building real projects

I was a field service engineer and I wanted to learn to code, but work and family limited my options. When I found Boot.dev, the "Intro to Coding in JavaScript" course got me up and running immediately, and I knew I made the right decision as soon as I joined the Discord community.

- Özgür Yildirim from Germany

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.

Pointers can be used to:

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.

Learn modern Python by writing Python code

Python has been growing in popularity for over 20 years, and today is used by tens of thousands of companies to deliver reliable software. If you are interested in a career in Python get started by writing real code with us.

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

11. Does Go support generic programming?

Go doesn’t currently support generics, but support is coming in 1.18. Generics will allow us as developers to write code that operates on types without needing to re-implement the same function multiple times for all the different types.

For example, say you want to implement a Binary Tree in Go, that can store any type of item. This should be easy, because the binary tree shouldn’t care about the types it stores, it never needs direct access to them. Currently, you only have two options: rewrite the tree for every type you want to support, or use an interface and cast the type every time you insert or remove something from the tree, neither of which are ideal solutions.

12. Is nil only valid on pointer types?

Nope! nil is the zero value for pointers, interfaces, maps, slices, channels, and function types. nil represents an uninitialized value.

13. Does Go provide support for OOP via classes?

No, Go isn’t an object-oriented programming language. Interfaces are the way we handle most abstractions in Go, and it requires an entirely different way of thinking.

14. How do you export functions from a package?

Exported function in Go just need to be capitalized.

func DoWork(){
    // this function is usable outside this package
}

func doWork(){
    // this function is NOT usable outside this package
}

15. What do you like about Go?

Well, this one’s up to you, but I have a few things in mind.

Learn to code by building real projects

Related Reading