0
0
Goprogramming~20 mins

Go modules overview - Practice Problems & Coding Challenges

Choose your learning style9 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.