Learn the essentials of Go modules and packages, including how to create and manage modules, import custom and standard packages, and understand package organization and conventions for efficient Go programming.
Creating and Managing Modules
What command is used to initialize a new Go module? a) go init b) go start c) go mod init d) go create module
What file does Go create when you initialize a module using go mod init? a) go.mod b) go.config c) go.package d) go.setup
Which of the following is used to download a module’s dependencies? a) go get b) go fetch c) go install d) go modules
What command can be used to update the dependencies listed in the go.mod file? a) go mod update b) go get -u c) go install d) go mod fix
What command removes unused dependencies from the go.mod file? a) go mod tidy b) go mod prune c) go mod clear d) go clean
What is the purpose of the go.sum file? a) To store metadata about the module b) To track the versions of modules and their dependencies c) To list all the available functions in a module d) To store module installation paths
How do you download a specific version of a module? a) go mod get <module>@<version> b) go fetch <module>@<version> c) go install <module>@<version> d) go update <module>@<version>
What command helps to check if all the dependencies are correctly fetched and available? a) go mod check b) go mod verify c) go mod install d) go get
Which command allows you to list all the available modules for a Go project? a) go list b) go show c) go mod list d) go modules
What happens when you run go mod tidy? a) It removes unnecessary files from the module b) It downloads all dependencies c) It removes unused dependencies from go.mod d) It compiles the Go program
Importing and Using Custom and Standard Packages
What is the correct syntax to import a package in Go? a) import <package> b) import "<package>" c) include "<package>" d) use "<package>"
Which of these is an example of importing a custom package? a) import "math" b) import "fmt" c) import "myproject/utils" d) import <myproject/utils>
Which Go standard package is used to format input and output? a) math b) fmt c) strings d) os
How can you import multiple packages in Go? a) import ("fmt" "math") b) import ("fmt", "math") c) import [fmt, math] d) import "fmt", "math"
What is the best practice when importing packages in Go? a) Only import the packages that are directly used in the code b) Always import all available packages c) Import packages in alphabetical order d) Import packages with full paths only
How can you give an alias to an imported package? a) import math as m b) import "math" as m c) import m "math" d) import m := "math"
What is the purpose of the blank identifier (_) when importing packages? a) It imports a package without using it in the program b) It imports a package and makes it accessible globally c) It skips the initialization of the package d) It imports a package but suppresses its functions
In Go, which of these functions is part of the strings package? a) Join b) Print c) Create d) Append
What is the command used to update the dependencies for a Go module? a) go install b) go update c) go mod update d) go get
How do you check the documentation for an imported package in Go? a) go doc <package> b) go help <package> c) go show <package> d) go list <package>
Package Organization and Conventions
What is the recommended convention for naming a Go package? a) Lowercase, short, and descriptive names b) Camel case c) Snake case d) Uppercase, long names
Where should Go source files be placed in a project? a) In the src directory b) In the bin directory c) In the go_modules directory d) In the root directory
Which of the following is a common convention for organizing Go files in a project? a) Use the main.go file for all program logic b) Place each function in a separate file c) Organize code by functionality into different directories d) Store all source files in one directory
What does the main package indicate in Go? a) It is used for creating libraries b) It is the entry point of a Go application c) It is a standard library package d) It is used for unit testing
Where should tests be placed in a Go project? a) In the test folder b) In the same directory as the source files with a .go extension c) In a separate repository d) In the bin folder
How do you define a Go package in a source file? a) package <name> b) package <project> c) define <package> d) set package <name>
Which command is used to format Go code according to Go conventions? a) go fmt b) go format c) go style d) go pretty
What is the purpose of the go test command? a) To run the Go application b) To test if Go is installed correctly c) To run the unit tests for the Go program d) To build the Go program
What should be included in the README.md file of a Go project? a) Instructions for compiling and running the project b) A list of Go functions c) A list of external modules d) A list of errors in the code
Which directory convention is often used in Go to store documentation? a) /docs b) /readme c) /doc d) /src
Answer Key
QNo
Answer (Option with text)
1
c) go mod init
2
a) go.mod
3
a) go get
4
b) go get -u
5
a) go mod tidy
6
b) To track the versions of modules and their dependencies
7
a) go mod get <module>@<version>
8
b) go mod verify
9
c) go mod list
10
c) It removes unused dependencies from go.mod
11
b) import "<package>"
12
c) import "myproject/utils"
13
b) fmt
14
a) import ("fmt" "math")
15
a) Only import the packages that are directly used in the code
16
c) import m "math"
17
a) It imports a package without using it in the program
18
a) Join
19
d) go get
20
a) go doc <package>
21
a) Lowercase, short, and descriptive names
22
a) In the src directory
23
c) Organize code by functionality into different directories
24
b) It is the entry point of a Go application
25
b) In the same directory as the source files with a .go extension
26
a) package <name>
27
a) go fmt
28
c) To run the unit tests for the Go program
29
a) Instructions for compiling and running the project