Level: IntermediateInterview

Go Modules Interview Questions: Packages and Versions (Part 10)

Updated 9 min read
Go Modules Interview Questions featured image: go.mod, internal folder and major version cards

These Go modules interview questions cover packages, imports, visibility, go.mod, go.sum, semantic versions, project layout and the go tool. We gathered them from public interview lists, coding rounds and community threads. Then we rewrote them in plain words and grouped them by idea.

This post is questions only, so answer each one in your own words first. When you need help, read Go modules and packages explained and install Go and run your first program. Also, finish Part 9 on error handling first, because exported errors are part of every package API.

How to Use These Go Modules Interview Questions

Answer each of these Go modules interview questions out loud, then try the commands in a scratch folder. Module problems often show up only in CI or on a new laptop. Finally, try the scenario questions, since they come from real build failures.

The table below lists each section and its number of questions.

SectionWhat it testsQuestions
Package basicsPackages, imports and visibility15
Module basicsgo.mod, go.sum and module paths15
Versions and dependenciesSemantic versions, MVS and upgrades15
Project layoutcmd, internal and workspaces14
Go tool commandsbuild, tidy, vet and vendor15
Package coding tasksHands-on module tasks14
Go Modules Interview Questions diagram showing six question areas from packages to the go tool
The six areas covered in this question list, from package basics to the go tool.

Go Modules Interview Questions on Packages

Before you answer, study Go modules and packages explained. These Go modules interview questions start with packages, because every Go file belongs to one.

  1. What is a package in Go, and how does it relate to a folder?
  2. Why must every file in one folder use the same package name?
  3. What is special about package main and the main function?
  4. How does Go decide whether a name is exported or not?
  5. Why does Go use a capital letter instead of keywords such as public and private?
  6. What is an import path, and how is it different from the package name?
  7. How do you give an imported package a different local name?
  8. What does a blank import such as import _ “image/png” do?
  9. Why does Go refuse to compile when an import is unused?
  10. Why are import cycles not allowed, and how do you break one?
  11. When does an init function run, and can a file have more than one?
  12. In what order does Go initialise package level variables and init functions?
  13. How should you name a package, for example short and lower case with no underscores?
  14. Scenario: when two packages import each other and the build fails, how would you restructure them?
  15. Scenario: when a package named utils keeps growing, how would you split it into clearer packages?

Go Modules Interview Questions on go.mod and go.sum

Before you answer, study install Go and run your first program for the go mod init step. These Go modules interview questions check what each module file really does.

  1. What is a Go module, and how is it different from a package?
  2. What does go mod init create, and how do you choose the module path?
  3. Which lines can appear in a go.mod file, for example module, go, require and replace?
  4. What does the go directive in go.mod control?
  5. What does the toolchain directive do in newer Go versions?
  6. Why does go.sum exist, and should you commit it to version control?
  7. What is the checksum database, and how does GOSUMDB use it?
  8. What is GOPROXY, and why does the default proxy make builds faster?
  9. How do you work with private modules, for example with GOPRIVATE?
  10. What does an indirect comment mean next to a requirement?
  11. How does a replace directive help when you test a local fork?
  12. Why should you remove replace directives before you tag a release?
  13. What is the module cache, and where does it live on disk?
  14. Scenario: when a build on CI fails with a checksum mismatch, what would you check first?
  15. Scenario: when a teammate cannot download a private module, how would you set up the environment?

Go Modules Interview Questions on Versions

Before you answer, study Go modules and packages explained again. Version questions show whether you can keep a real project stable, so interviewers add them to most Go modules interview questions lists.

  1. What is semantic versioning, and what do the three numbers mean?
  2. Why does a major version of v2 or higher need a new import path?
  3. How do you publish a v2 module, using a subfolder or a branch?
  4. What is minimal version selection, and how does it pick a version?
  5. How is minimal version selection different from the newest-version rule in other tools?
  6. What is a pseudo-version, and when does Go create one?
  7. How do you upgrade one dependency to its latest minor version?
  8. How do you downgrade a dependency or remove it completely?
  9. What does a retract directive do, and when would a maintainer use it?
  10. What does the +incompatible suffix mean on a version?
  11. How do you see why a module is in your build, for example with go mod why?
  12. How do you list every module and version in your build?
  13. Why should you read release notes before a major upgrade?
  14. Scenario: when two dependencies need different minor versions of the same module, which one does Go choose, and why?
  15. Scenario: when a security advisory lists one of your modules, how would you find and fix it, for example with govulncheck?

Project Layout and Workspaces

Before you answer, study Go clean architecture. These Go modules interview questions test how you shape a real project, so explain the reason behind each folder.

  1. What is the purpose of a cmd folder in a Go project?
  2. How does an internal folder limit which packages can import it?
  3. Why do many teams avoid a pkg folder in new projects?
  4. Should a small service have many packages or just a few?
  5. How do you organise packages by feature instead of by layer?
  6. Where should you put test helpers that several packages share?
  7. What is a Go workspace, and what does go.work do?
  8. When would you use go work instead of replace directives?
  9. Should you commit go.work to version control, and why or why not?
  10. How do you keep a monorepo with several modules easy to build?
  11. How do you embed static files into a binary with the embed package?
  12. What are build tags, and how do they select files for a platform?
  13. Scenario: when a new hire cannot find where the HTTP handlers live, how would you restructure the project?
  14. Scenario: when another team imports your internal helpers and blocks your changes, how would you protect them?

Go Modules Interview Questions on the Go Tool

Before you answer, study Go cross compilation and Go Docker deployment. These Go modules interview questions check the commands you use every day.

  1. What does go build do, and where does it put the binary?
  2. How is go run different from go build?
  3. What does go install do with a version suffix such as @latest?
  4. What does go mod tidy add and remove?
  5. Why should CI fail when go mod tidy changes go.mod?
  6. What does go mod vendor do, and when do teams still use vendoring?
  7. What does go vet check, and how is it different from the compiler?
  8. How do you format code with gofmt, and why does Go have one official style?
  9. How do you cross compile with GOOS and GOARCH?
  10. What does CGO_ENABLED=0 change in the final binary?
  11. How do you put a version string into a binary with -ldflags?
  12. How does the build cache speed up repeated builds?
  13. What does go env show, and how do you change one value for good?
  14. Scenario: when a Docker build downloads every module on each run, how would you cache them?
  15. Scenario: when a binary works on your Mac but fails in an Alpine container, what would you check?

Go Modules Interview Questions for Coding Rounds

Before you answer, write each task in a fresh folder. Interviewers watch how you set up the module and name packages, not only whether the code runs.

  1. Create a new module, add one package and call it from main.
  2. Split a single file program into a main package and a store package.
  3. Add a third-party module, then pin it to an exact version.
  4. Replace a dependency with a local copy for testing, then undo it.
  5. Write an init function that checks a required environment variable.
  6. Read the module version at run time with runtime/debug.ReadBuildInfo.
  7. Build the same program for Linux, Windows and macOS.
  8. Set up a go.work file for two local modules.
  9. Move private helpers into an internal folder and fix the imports.
  10. Write a Makefile target that runs tidy, vet and tests.
  11. Embed a config file into the binary and print it.
  12. Publish a tiny v2 module with the right import path.
  13. Scenario: when an interviewer gives you a project with an import cycle, how would you find and remove it?
  14. Scenario: when a release needs a version number in the binary, how would you add it during the build?

Code Reading Questions

Read each program carefully and predict the output before you run it. Then explain the result in your own words.

package main

import "fmt"

var a = b + 1
var b = initB()

func initB() int {
    fmt.Println("initB runs")
    return 10
}

func init() {
    fmt.Println("first init, a =", a)
}

func init() {
    fmt.Println("second init")
}

func main() {
    fmt.Println("main, a + b =", a+b)
}
  1. In what order do the four lines print, and why does b get its value before a?
  2. What would happen if a and b each depended on the other?

The second program shows how export rules reach across packages.

package main

import (
    "encoding/json"
    "fmt"
)

type Config struct {
    Name    string `json:"name"`
    port    int
    Debug   bool   `json:"debug,omitempty"`
    Version string `json:"-"`
}

func main() {
    c := Config{Name: "api", port: 8080, Version: "v1.2.0"}
    out, _ := json.Marshal(c)
    fmt.Println(string(out))
    var d Config
    _ = json.Unmarshal([]byte(`{"name":"web","port":9000,"debug":true}`), &d)
    fmt.Println(d.Name, d.port, d.Debug)
}
  1. What JSON does the first print line show, and why are three fields missing?
  2. Why does d.port stay 0 after the decode, even though the JSON has a port value?

Quick Revision Table for Go Modules

Use the table below as a final check before your interview. It lists the commands that interviewers ask about most.

CommandWhat it doesTypical use
go mod initCreates go.modStart a new module
go get pkg@v1.4.0Adds or changes a dependencyUpgrade or pin a version
go mod tidyAdds missing and removes unused modulesBefore every commit
go mod whyExplains why a module is neededTrim the build
go list -m allLists every module in the buildAudit versions
go work initCreates a workspaceEdit two modules together

Also, many interviewers ask you to compare the two module files directly. The diagram below shows the difference side by side.

Go modules diagram comparing the go.mod file with the go.sum file
go.mod declares what the module needs, while go.sum stores checksums that keep builds safe.

Official Resources

These official pages explain the rules behind these questions.

Quick Recap

  • These Go modules interview questions cover packages, module files, versions, layout, the go tool and coding tasks.
  • A package is one folder of code, while a module is a versioned set of packages.
  • A capital first letter exports a name, so lower case names stay private.
  • Major versions from v2 need a new import path, and Go picks versions with minimal version selection.
  • Commit go.sum, run go mod tidy often and keep replace lines out of releases.
  • Use internal folders to protect code that other teams should not import.

What’s Next?

By now, you have Go modules interview questions for every part of packages and versions. Study the answers in your own notes, then check them against the Go interview topics study guide.

Next up: Go generics interview questions (Part 11)