Go (Golang) interview questions

26 questions from the Go (Golang) course, each with the correct answer and an explanation. Try to answer before you open the answer.

Getting Started With Go

1. What does a Go build typically produce?

  1. Java bytecode
  2. A folder of interpreted scripts
  3. A single compiled binary
  4. A Docker image automatically
Show answer

Answer: A single compiled binary

Static binaries are easy to deploy.

Lesson: Why Go

2. Where does a Go program start running?

  1. func main() in package main
  2. The first function in any file
  3. init.go
  4. A class with a static main method
Show answer

Answer: func main() in package main

package main + func main.

Lesson: Hello, World and the go Command

3. How is an identifier exported from a Go package?

  1. By putting it in main.go
  2. With the export keyword
  3. By listing it in go.mod
  4. By starting its name with an uppercase letter
Show answer

Answer: By starting its name with an uppercase letter

Capitalisation controls visibility.

Lesson: Modules and Packages

Language Basics

4. What is the zero value of a string in Go?

  1. nil
  2. The empty string ""
  3. "0"
  4. undefined
Show answer

Answer: The empty string ""

Every type has a defined zero value.

Lesson: Variables, Constants and Zero Values

5. How do you write a while loop in Go?

  1. while condition { ... }
  2. for condition { ... }
  3. do { } while
  4. loop until condition
Show answer

Answer: for condition { ... }

for covers every loop form.

Lesson: Control Flow

6. What is the idiomatic way for a Go function to report failure?

  1. Exit the program
  2. Throw an exception
  3. Print to stderr and return 0
  4. Return an error value as the last result
Show answer

Answer: Return an error value as the last result

Errors are values.

Lesson: Functions, Multiple Returns and Closures

7. What does v, ok := m["key"] tell you when ok is false?

  1. The key is not in the map, and v is the zero value
  2. The map is nil and will panic
  3. The value is false
  4. The map is sorted
Show answer

Answer: The key is not in the map, and v is the zero value

Comma-ok distinguishes missing from zero.

Lesson: Slices and Maps

Structs, Interfaces and Generics

8. Why did BrokenDeposit not change the balance?

  1. The balance field is unexported
  2. Go structs are immutable
  3. It has a value receiver, so it modified a copy
  4. Methods cannot change fields
Show answer

Answer: It has a value receiver, so it modified a copy

Pointer receivers modify the original.

Lesson: Structs and Methods

9. How does a Go type satisfy an interface?

  1. By embedding the interface name in its struct
  2. With an implements clause
  3. By having all the interface's methods; no declaration is needed
  4. By registering in go.mod
Show answer

Answer: By having all the interface's methods; no declaration is needed

Satisfaction is implicit.

Lesson: Interfaces

10. What does the constraint cmp.Ordered allow?

  1. Types that support < and > comparisons
  2. Any type at all
  3. Only strings
  4. Only types with a String method
Show answer

Answer: Types that support < and > comparisons

Constraints limit type parameters.

Lesson: Generics

Errors, defer and panic

11. Which verb in fmt.Errorf wraps an error so errors.Is can find it?

  1. %e
  2. %v
  3. %s
  4. %w
Show answer

Answer: %w

%w preserves the chain.

Lesson: Wrapping and Inspecting Errors

12. In what order do deferred calls run?

  1. First in, first out
  2. Last in, first out
  3. Randomly
  4. Alphabetically
Show answer

Answer: Last in, first out

Like a stack.

Lesson: defer, panic and recover

13. Why avoid both logging and returning the same error?

  1. Returning errors is forbidden
  2. It produces duplicate log entries as the error moves up
  3. Logs cannot contain errors
  4. It changes the error type
Show answer

Answer: It produces duplicate log entries as the error moves up

Handle an error once.

Lesson: Designing Errors

Concurrency

14. Does main wait for goroutines it started before exiting?

  1. Only if they print output
  2. Yes, always
  3. Only for the first goroutine
  4. No, you must wait explicitly, for example with a WaitGroup
Show answer

Answer: No, you must wait explicitly, for example with a WaitGroup

Coordinate completion explicitly.

Lesson: Goroutines and WaitGroups

15. What ends a for v := range ch loop?

  1. The channel being closed and drained
  2. Receiving a zero value
  3. A timeout by default
  4. The buffer becoming full
Show answer

Answer: The channel being closed and drained

Close signals completion.

Lesson: Channels and Pipelines

16. What does ctx.Done() return?

  1. A boolean
  2. An error value
  3. A channel that is closed when the context is cancelled or times out
  4. The remaining time
Show answer

Answer: A channel that is closed when the context is cancelled or times out

Select on Done to stop work.

Lesson: select, Timeouts and context

17. What is a data race?

  1. A slow network request
  2. Two goroutines reading the same constant
  3. Concurrent access to the same memory with at least one write and no synchronisation
  4. A channel with a buffer
Show answer

Answer: Concurrent access to the same memory with at least one write and no synchronisation

Use mutexes, atomics or channels.

Lesson: Data Races, Mutexes and the Race Detector

The Standard Library for Services

18. What does the pattern "GET /products/{id}" do with a POST request?

  1. The mux responds 405 Method Not Allowed
  2. Calls the handler anyway
  3. Returns 404 always
  4. Crashes the server
Show answer

Answer: The mux responds 405 Method Not Allowed

Method-aware routing since Go 1.22.

Lesson: HTTP Servers With net/http

19. What does the tag json:"-" do?

  1. Encrypts it
  2. Makes the field required
  3. Renames it to "-"
  4. Excludes the field from JSON encoding and decoding
Show answer

Answer: Excludes the field from JSON encoding and decoding

Keep internal fields out of payloads.

Lesson: JSON With Struct Tags

20. What does the layout "2006-01-02" mean in Go?

  1. The date the code was written
  2. Year-month-day, written using the reference date
  3. A fixed date constant to display
  4. An invalid layout
Show answer

Answer: Year-month-day, written using the reference date

Layouts use the reference time.

Lesson: Time and Formatting

Testing and Tooling

21. Why use t.Run for each table case?

  1. It makes tests run in production
  2. Each case becomes a named subtest, so failures point to the exact case
  3. It disables coverage
  4. It is required to compile
Show answer

Answer: Each case becomes a named subtest, so failures point to the exact case

Named, isolated cases.

Lesson: Table-Driven Tests

22. What does go vet report?

  1. Dependency updates
  2. Only syntax errors
  3. Performance benchmarks
  4. Code that compiles but is likely a bug
Show answer

Answer: Code that compiles but is likely a bug

Static analysis for common mistakes.

Lesson: go vet

23. Why does every Go project use gofmt?

  1. It enforces one standard style automatically, ending style debates
  2. It makes code run faster
  3. It is required to compile
  4. It removes comments
Show answer

Answer: It enforces one standard style automatically, ending style debates

Consistency at zero cost.

Lesson: gofmt and Consistent Style

Building Real Services

24. Where do Go projects commonly put private application packages?

  1. In go.sum
  2. Under public/
  3. Under internal/
  4. Inside the GOPATH bin folder
Show answer

Answer: Under internal/

internal/ cannot be imported by other modules.

Lesson: Project Structure and Dependencies

25. What does http.Server.Shutdown do?

  1. Restarts the server
  2. Kills all connections immediately
  3. Stops accepting new connections and waits for in-flight requests to finish
  4. Deletes log files
Show answer

Answer: Stops accepting new connections and waits for in-flight requests to finish

Graceful draining.

Lesson: Graceful Shutdown and Observability

26. Which item belongs on a Go review checklist?

  1. Tests run with the -race flag in CI
  2. Ignore errors with _ by default
  3. Rely on map iteration order
  4. Use value receivers for mutating methods
Show answer

Answer: Tests run with the -race flag in CI

Correct, synchronised, formatted.

Lesson: A Go Code Review Checklist

← Back to the course