Sample questions
Question 1Easy
What does gofmt (or go fmt) do?
- Downloads dependencies
- Finds data races and other concurrency bugs by running the code under instrumentation
- Formats code in the standard Go style
- Generates documentation websites
Answer: C. Formats code in the standard Go style
Explanation
One standard format ends style debates; most editors run it on save.
Step-by-step solution
- Gofmt is a tool that formats Go code automatically.
- It ensures all code follows the same style and formatting rules.
- This makes code easier to read and reduces style debates among developers.
- Most Go editors run gofmt on save to keep code clean.
- The main purpose of gofmt is to format code in the standard Go style.
- Final answer: Formats code in the standard Go style.
Why the other options are wrong
- A. Downloading dependencies is done by go get or go mod download, not by gofmt.
- B. Finding data races is done by go test -race or go run -race, not by gofmt.
- D. Documentation sites come from go doc or godoc tools, not from gofmt.
Tip: Use gofmt to automatically format your Go code and maintain consistency.
Question 2Easy
What does the go.mod file define?
- The module path, Go version and dependency requirements
- Environment variables
- The compiled binaries of each dependency, so builds work without network access
- Test results
Answer: A. The module path, Go version and dependency requirements
Explanation
It is created by go mod init and is the root of a module.
Step-by-step solution
- go.mod defines module path, Go version, and dependencies.
- It ensures correct package paths and versions.
- Dependencies are listed for build consistency.
- Go version specifies which compiler to use.
- Module path identifies the module's location.
Why the other options are wrong
- B. Environment variables are set outside go.mod, not in it.
- C. go.mod stores no compiled binaries; dependencies are module source, not build output.
- D. Test results are not stored in go.mod; go test prints them.
Tip: go.mod is essential for managing dependencies and ensuring consistent builds in Go projects.
Question 3Easy
What does go mod tidy do?
- Deletes the module cache
- Adds missing and removes unused dependencies
- Runs all tests
- Formats source code and sorts the imports in every file of the module
Answer: B. Adds missing and removes unused dependencies
Explanation
It syncs go.mod and go.sum with the imports actually used.
Step-by-step solution
- Go mod tidy checks dependencies in go.mod and go.sum.
- It adds any missing dependencies needed for the project.
- It removes dependencies not used in the code anymore.
- This keeps the dependency list clean and up to date.
- The result is a synced go.mod and go.sum file.
- Final answer: Adds missing and removes unused dependencies.
Why the other options are wrong
- A. tidy does not delete the module cache; that is done with go clean -modcache.
- C. Tests are not run by go mod tidy; use go test for that.
- D. Formatting is done by gofmt, not go mod tidy, which only edits module files.
Tip: Use go mod tidy to keep dependencies up to date and remove unused ones.
Question 4Easy
An executable Go program must have:
- A class named Main
- A
main.go file in every folder - An
init function in every package package main with a func main()
Answer: D. package main with a func main()
Explanation
go build on package main produces a binary; other packages are libraries.
Step-by-step solution
- Go programs need
package main to run as standalone executables. func main() defines the entry point of the program.- Combining
package main with func main() makes a valid executable. - Other options are not required for an executable Go program.
- Correct answer is
package main with a func main().
Why the other options are wrong
- A. Go has no classes, so a class named Main is not needed for a program.
- B. main.go is only a common file name; any file name works in package main.
- C. init is optional; an executable only needs func main, not an init in every package.
Tip: Remember: package main + func main() = executable Go program.
Question 5Easy
Why is Go popular for cloud and DevOps tools such as container runtimes and CLIs?
- It runs only on Windows
- It compiles to single static binaries with built-in concurrency
- It has no standard library
- It needs a heavy virtual machine at runtime, which makes deployment more uniform
Answer: B. It compiles to single static binaries with built-in concurrency
Explanation
Easy cross-compilation and simple deployment make Go a common choice for infrastructure software.
Step-by-step solution
- Go compiles to single static binaries that are easy to ship and run.
- Goroutines and channels give concurrency built into the language.
- So option B explains why Go is popular for cloud and DevOps tools.
Why the other options are wrong
- A. Go runs on Windows, Linux and macOS, not only Windows.
- C. Go has a standard library, so this is false.
- D. Go does not need a heavy virtual machine at runtime.
Tip: Go suits tools because it gives one binary to deploy and easy concurrency.
Question 6Medium
A package inside an 'internal' directory can be imported by:
- Only test files
- Nobody; internal packages cannot be imported
- Only code rooted at the parent of internal
- Any module anywhere
Answer: C. Only code rooted at the parent of internal
Explanation
internal enforces encapsulation for packages not meant as public API.
Step-by-step solution
- Internal directory restricts access to its contents.
- Code outside internal can import it if rooted at parent level.
- Import path starts from parent of internal directory.
- Only code in parent or higher directories can import internal packages.
- This ensures controlled access without exposing internals publicly.
Why the other options are wrong
- A. Test files are not the only allowed importers; the rule is about the parent directory.
- B. Internal packages can be imported by code under their parent, so nobody is wrong.
- D. The internal rule blocks any module; only code under the parent may import it.
Tip: Remember: internal packages are visible only to code in the parent directory or above.
Question 7Medium
Under semantic import versioning, how is major version 2 of a module imported?
- With /v2 at the end of the module path
- With @latest in the import
- By renaming the package to name2 and keeping the module path unchanged in go.mod
- Exactly the same path as v1
Answer: A. With /v2 at the end of the module path
Explanation
Major versions with breaking changes get a new import path, e.g. example.com/lib/v2.
Step-by-step solution
- Semantic import versioning ties the major version to the import path.
- For v2 and above the module path ends in /v2.
- Imports then read example.com/mod/v2.
Why the other options are wrong
- B. @latest is a version query for go get, not part of an import path.
- C. Renaming the package does not change the module path rules.
- D. The same path would mean v1; v2 would conflict.
Question 8Medium
- Reports suspicious constructs like wrong Printf verbs or copied locks
- Upgrades dependencies
- Formats code
- Builds a Docker image for the module and checks that it starts without errors
Answer: A. Reports suspicious constructs like wrong Printf verbs or copied locks
Explanation
vet finds likely bugs that compile fine; it runs automatically as part of go test for a subset of checks.
Step-by-step solution
- go vet is a static analyser.
- It finds suspicious code.
- E.g. Printf verb mismatches.
Why the other options are wrong
- B. go get/mod upgrade dependencies.
- C. gofmt formats.
- D. vet does not build Docker images.
Question 9Hard
Go generics: what does func Max[T cmp.Ordered](a, b T) T declare?
- A method on a type named T
- A function that returns an error
- A function that accepts only interface values and converts them with type assertions
- A generic function over any ordered type such as int, float64 or string
Answer: D. A generic function over any ordered type such as int, float64 or string
Explanation
Type parameters with constraints let one function work across types while staying type-safe.
Step-by-step solution
- Func Max[T cmp.Ordered](a, b T) T declares a type parameter T in square brackets.
- The constraint cmp.Ordered limits T to ordered types such as int, float64 and string.
- The function takes two T values and returns a T, so it works for any ordered type.
- So option D is the correct description.
Why the other options are wrong
- A. Max is a plain function, not a method on a type named T.
- B. It returns a T, not an error.
- C. Generics use type parameters, not interface values with type assertions.
Tip: Square brackets after a function name declare type parameters; the constraint says which types are allowed.
Question 10Hard
How do you build a Linux amd64 binary from a Mac?
- You must build on a Linux machine
go build --target=linuxGOOS=linux GOARCH=amd64 go buildgo cross linux
Answer: C. GOOS=linux GOARCH=amd64 go build
Explanation
Go cross-compiles by setting the GOOS and GOARCH environment variables.
Step-by-step solution
- Set GOOS to linux with
GOOS=linux. - Set GOARCH to amd64 with
GOARCH=amd64. - Run
go build to compile for Linux amd64. - This sets the target OS and architecture correctly.
- The binary is built without needing a Linux machine.
- Final answer:
GOOS=linux GOARCH=amd64 go build.
Why the other options are wrong
- A. No Linux machine is needed; Go cross-compiles from a Mac using GOOS and GOARCH.
- B. go build has no --target flag; cross-compiling uses the GOOS and GOARCH variables.
- D. go cross is not a Go command; use go build with GOOS and GOARCH set.
Tip: Use GOOS=linux GOARCH=amd64 go build for Linux binaries from macOS.
Practise all 12 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →