Challenge - 5 Problems
Go Modules Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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?Attempts:
2 left
💡 Hint
The
go mod init command creates a go.mod file with the module path and Go version.✗ Incorrect
The
go.mod file starts with the module path you specify and the Go language version. The correct format is module example.com/mymodule followed by go 1.20 (or your Go version).❓ Predict Output
intermediate2: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?Attempts:
2 left
💡 Hint
Think about what
go get does in module mode.✗ Incorrect
go get adds the dependency to go.mod with a require statement and updates go.sum with cryptographic hashes to verify module integrity.🔧 Debug
advanced2: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?Attempts:
2 left
💡 Hint
Check if the dependency is declared in your module file.
✗ Incorrect
Go modules require that all external packages be listed in
go.mod under require. If you import a package not declared or downloaded, Go cannot find it.📝 Syntax
advanced2: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?Attempts:
2 left
💡 Hint
The replace directive uses the syntax: replace [old module] [old version] => [new module or path]
✗ Incorrect
The correct syntax for replace is:
replace example.com/old v1.0.0 => ../local/old
No version is needed for the local path.
🚀 Application
expert3: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?Attempts:
2 left
💡 Hint
go mod tidy cleans unused dependencies from go.mod.✗ Incorrect
go mod tidy removes dependencies not used in the code. Since golang.org/x/net is not imported, it will be removed from go.mod and not downloaded.