Bird
Raised Fist0
Goprogramming~20 mins

Go modules overview - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Go Modules Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Go module initialization command?
You run the command go mod init example.com/mymodule in an empty directory. What will be the content of the go.mod file?
A
module mymodule

go 1.20
B
module example.com/mymodule

go 1.20
C
module example.com/mymodule

require go 1.20
D
module example.com/mymodule

version 1.20
Attempts:
2 left
💡 Hint
The go mod init command creates a go.mod file with the module path and Go version.
❓ Predict Output
intermediate
2:00remaining
What is the output of this Go command to add a dependency?
You run go get github.com/some/dependency@v1.2.3 in a module-enabled project. What happens to the go.mod and go.sum files?
AThe <code>go.mod</code> file adds a require line for the dependency and <code>go.sum</code> records checksums.
BNothing changes until you run <code>go mod tidy</code>.
CThe <code>go.mod</code> file adds a replace directive for the dependency version.
DThe <code>go.mod</code> file is deleted and <code>go.sum</code> is unchanged.
Attempts:
2 left
💡 Hint
Think about what go get does in module mode.
🔧 Debug
advanced
2:00remaining
Why does this Go build fail with a module error?
You have a go.mod file with module example.com/mymodule. Your code imports example.com/othermodule/pkg but you get an error: cannot find module providing package example.com/othermodule/pkg. What is the cause?
AThe Go version in <code>go.mod</code> is too old to support modules.
BThe import path is invalid because it uses dots instead of slashes.
CThe module <code>example.com/othermodule</code> is not required in <code>go.mod</code> and not downloaded.
DThe <code>go.sum</code> file is missing.
Attempts:
2 left
💡 Hint
Check if the dependency is declared in your module file.
📝 Syntax
advanced
2:00remaining
Which go.mod snippet correctly specifies a replace directive?
You want to replace the module example.com/old version v1.0.0 with a local path ../local/old. Which snippet is correct?
Adlo/lacol/.. >= 0.0.1v dlo/moc.elpmaxe ecalper
Breplace example.com/old v1.0.0 => example.com/local/old
Creplace example.com/old v1.0.0 => ../local/old v1.0.0
Dreplace example.com/old v1.0.0 => ../local/old
Attempts:
2 left
💡 Hint
The replace directive uses the syntax: replace [old module] [old version] => [new module or path]
🚀 Application
expert
3:00remaining
How many modules are downloaded after running go mod tidy on this project?
Given a go.mod file requiring github.com/pkg/errors v0.9.1 and golang.org/x/net v0.0.0-20210428140749-89ef3d95e781, and your code imports only github.com/pkg/errors, what will go mod tidy do?
AIt will remove <code>golang.org/x/net</code> from <code>go.mod</code> and download only <code>github.com/pkg/errors</code>.
BIt will keep both modules in <code>go.mod</code> and download both.
CIt will remove both modules from <code>go.mod</code> because they are not used directly.
DIt will add new modules for transitive dependencies but keep both original modules.
Attempts:
2 left
💡 Hint
go mod tidy cleans unused dependencies from go.mod.

Practice

(1/5)
1. What is the main purpose of Go modules in a Go project?
easy
A. To compile Go code faster
B. To manage project dependencies and versions easily
C. To replace the Go compiler
D. To write Go code without packages

Solution

  1. Step 1: Understand what Go modules do and compare options

    Go modules help manage dependencies and versions in a Go project. Only "To manage project dependencies and versions easily" correctly describes this purpose; others are unrelated.
  2. Final Answer:

    To manage project dependencies and versions easily -> Option B
  3. Quick Check:

    Go modules = dependency management [OK]
Hint: Remember: modules manage dependencies and versions [OK]
Common Mistakes:
  • Thinking modules speed up compilation
  • Confusing modules with compiler tools
  • Believing modules remove packages
2. Which command initializes a new Go module in your project directory?
easy
A. go mod init
B. go build
C. go run
D. go get

Solution

  1. Step 1: Identify the command to start a module and check others

    The command go mod init creates a new module in the current directory. go build compiles code, go run runs code, and go get fetches packages, so they don't initialize modules.
  2. Final Answer:

    go mod init -> Option A
  3. Quick Check:

    Initialize module = go mod init [OK]
Hint: Init module with 'go mod init' command [OK]
Common Mistakes:
  • Using 'go build' to start a module
  • Confusing 'go get' with module initialization
  • Trying 'go run' to create modules
3. What does the command go mod tidy do when run in a Go module project?
medium
A. It compiles the project
B. It deletes the go.mod file
C. It runs all tests
D. It removes unused dependencies and adds missing ones

Solution

  1. Step 1: Understand the purpose of 'go mod tidy' and compare options

    This command cleans the module dependencies by removing unused ones and adding any missing required dependencies. Options A, C, and D describe unrelated actions like compiling, testing, or deleting files.
  2. Final Answer:

    It removes unused dependencies and adds missing ones -> Option D
  3. Quick Check:

    go mod tidy = clean dependencies [OK]
Hint: Use 'go mod tidy' to clean dependencies [OK]
Common Mistakes:
  • Thinking it compiles code
  • Assuming it runs tests
  • Believing it deletes module files
4. You ran go mod init myproject but got an error saying "go.mod file already exists". What is the likely cause?
medium
A. The Go compiler is not installed
B. You need to run 'go mod tidy' first
C. You already have a go.mod file in the directory
D. You must delete the main.go file first

Solution

  1. Step 1: Understand the error and check other options

    The error "go.mod file already exists" means the module file is already present in the directory. Running 'go mod tidy' or deleting main.go won't fix this. The compiler installation is unrelated.
  2. Final Answer:

    You already have a go.mod file in the directory -> Option C
  3. Quick Check:

    Existing go.mod file causes this error [OK]
Hint: Check if go.mod already exists before init [OK]
Common Mistakes:
  • Trying to tidy before init
  • Assuming compiler missing causes error
  • Deleting unrelated files to fix error
5. You want to share your Go project with others and ensure they get the exact dependencies you used. Which two commands should you run to prepare your project? (Choose the best pair)
hard
A. go mod vendor and go mod tidy
B. go build and go run
C. go get and go test
D. go mod init and go mod tidy

Solution

  1. Step 1: Identify commands to prepare modules for sharing and evaluate options

    go mod vendor copies dependencies into the vendor directory, ensuring exact versions are included, and go mod tidy cleans dependencies to ensure correctness. go mod init creates the module file but is usually done once at project start. Other options do not prepare dependencies for sharing.
  2. Final Answer:

    go mod vendor and go mod tidy -> Option A
  3. Quick Check:

    Vendor + tidy prepare exact dependencies for sharing [OK]
Hint: Use 'go mod vendor' and 'go mod tidy' before sharing [OK]
Common Mistakes:
  • Using build or run commands for module setup
  • Confusing get or test with module preparation
  • Relying only on init and tidy without vendoring
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard