Golang Packages & Modules Quiz

Golang
0 Passed
0% acceptance

35 comprehensive questions on Golang packages and modules, covering package creation, exported vs unexported identifiers, Go modules usage, dependency management, and versioning rules — with 18 code examples demonstrating package organization and module management.

35 Questions
~70 minutes
1

Question 1

What is a package in Golang?

A
A collection of Go source files in the same directory
B
A single .go file
C
A compiled binary
D
A Go module
2

Question 2

How do you declare a package?

go
package main

import "fmt"

func main() {
    fmt.Println("Hello")
}
A
package packagename at top of every .go file
B
package "name" in go.mod
C
Use import statement
D
No declaration needed
3

Question 3

What is the difference between exported and unexported identifiers?

A
Exported: starts with capital letter, visible outside package
B
Exported: starts with lowercase, visible outside
C
No difference in visibility
D
Depends on package name
4

Question 4

What happens when you try to access an unexported identifier from another package?

go
package main

import "mypackage"

func main() {
    mypackage.unexportedFunc()  // Error
}
A
Compilation error - identifier not exported
B
Works at runtime
C
Warning but compiles
D
Depends on build tags
5

Question 5

What is a Go module?

A
A collection of Go packages with dependency management
B
A single package
C
A compiled library
D
A Go workspace
6

Question 6

How do you initialize a Go module?

bash
go mod init example.com/myproject
A
go mod init module-path
B
go init module-path
C
go mod create module-path
D
No initialization needed
7

Question 7

What is the go.mod file?

go
module example.com/myproject

go 1.21

require (
    github.com/some/lib v1.0.0
)
A
Module definition with dependencies and Go version
B
Package source code
C
Build configuration
D
Test file
8

Question 8

How do you add a dependency to a module?

bash
go get github.com/some/[email protected]
A
go get package@version
B
go add package@version
C
Edit go.mod manually
D
Use go mod add
9

Question 9

What does go mod tidy do?

A
Adds missing dependencies, removes unused ones
B
Formats go.mod file
C
Updates all dependencies
D
Creates vendor directory
10

Question 10

What is semantic versioning in Go modules?

A
vMAJOR.MINOR.PATCH with breaking changes in MAJOR
B
Any version numbering scheme
C
Only patch versions allowed
D
No versioning required
11

Question 11

In a team project, you're creating a shared utility package. How should you name exported functions?

A
Start with capital letter: ValidateEmail, not validateEmail
B
Use lowercase for all functions
C
Use underscores: validate_email
D
Name doesn't affect export
12

Question 12

What is the difference between internal and regular packages?

text
project/
├── internal/
│   └── utils/
│       └── helper.go
└── pkg/
    └── api/
        └── client.go
A
internal packages only importable by parent directories
B
internal packages are hidden
C
No difference
D
internal is deprecated
13

Question 13

How do you import a package?

go
package main

import (
    "fmt"
    "github.com/user/mylib"
)

func main() {
    fmt.Println(mylib.SomeFunc())
}
A
import "module/path" or "standard/package"
B
include "package"
C
use "package"
D
No import needed
14

Question 14

What is vendoring in Go?

A
Copying dependencies into vendor/ directory
B
Selling Go packages
C
Creating package documentation
D
Publishing to pkg.go.dev
15

Question 15

How do you update a dependency to latest version?

bash
go get github.com/some/package@latest
A
go get package@latest
B
go update package
C
go mod upgrade package
D
Edit go.mod manually
16

Question 16

What is the go.sum file?

A
Cryptographic checksums of dependencies
B
Summary of go.mod
C
Build output
D
Test results
17

Question 17

In a library you're publishing, how should you version releases?

A
Use git tags like v1.2.3 for releases
B
Use any tagging scheme
C
No tagging needed
D
Use branches for versions
18

Question 18

What is a replace directive in go.mod?

go
module example.com/myproject

go 1.21

require github.com/some/lib v1.0.0

replace github.com/some/lib => ./local/fork
A
Replace dependency with local version
B
Rename a dependency
C
Update dependency version
D
Remove dependency
19

Question 19

How do you create a package with multiple files?

text
mypackage/
├── utils.go
├── types.go
└── constants.go
A
Put all .go files in same directory with same package declaration
B
Use one file per package
C
Use subdirectories
D
Cannot split packages
20

Question 20

In a microservices architecture, you're creating shared packages. Should you use internal/ or pkg/ for shared utilities?

A
pkg/ for public APIs, internal/ for private implementation
B
internal/ for all shared code
C
pkg/ for all shared code
D
No difference
21

Question 21

What is the GOPROXY environment variable?

A
URL of Go module proxy for downloading dependencies
B
Path to Go installation
C
Go workspace directory
D
Go binary path
22

Question 22

How do you handle incompatible dependency versions?

bash
go mod edit -require=github.com/[email protected]
A
Use go mod edit to specify exact versions
B
Use replace directive
C
Cannot handle incompatible versions
D
Use go get with force flag
23

Question 23

What is the difference between go.mod and go.sum?

A
go.mod: dependencies, go.sum: checksums for security
B
go.mod: checksums, go.sum: dependencies
C
They are identical
D
go.sum is optional
24

Question 24

How do you publish a Go module?

bash
git tag v1.0.0
git push origin v1.0.0
A
Create git tag with v prefix and push
B
Upload to pkg.go.dev
C
Use go mod publish
D
No publishing needed
25

Question 25

What is a major version change in semantic versioning?

A
Breaking API changes require v2, v3, etc.
B
Any new features
C
Bug fixes only
D
No major versions in Go
26

Question 26

How do you organize a large Go project?

text
project/
├── cmd/
│   └── server/
├── internal/
│   └── database/
├── pkg/
│   └── api/
└── go.mod
A
cmd/ for binaries, internal/ for private, pkg/ for public
B
All code in root directory
C
Use submodules
D
No organization needed
27

Question 27

What happens when you import an unused package?

go
package main

import (
    "fmt"
    "unused/package"  // Not used
)

func main() {
    fmt.Println("Hello")
}
A
Compilation error - unused import
B
Warning but compiles
C
Works fine
D
Runtime error
28

Question 28

How do you import for side effects only?

go
import _ "github.com/lib/drivers"
A
import _ "package" - blank identifier ignores import
B
import "package" as usual
C
Cannot import for side effects
D
Use import side "package"
29

Question 29

What is the GOSUMDB environment variable?

A
Checksum database for verifying dependencies
B
Go source directory
C
Module cache location
D
Go binary database
30

Question 30

In a CI/CD pipeline, how should you cache Go dependencies?

A
Cache $GOPATH/pkg/mod and go.sum
B
Cache entire GOPATH
C
No caching needed
D
Cache source code only
31

Question 31

What is the difference between require and replace in go.mod?

go
require github.com/lib v1.0.0
replace github.com/lib => ./fork
A
require: needed dependency, replace: use different version/path
B
require: local path, replace: remote
C
They are identical
D
replace is deprecated
32

Question 32

How do you work with private modules?

bash
go env -w GOPRIVATE=example.com/private/*
A
Set GOPRIVATE to skip proxy/checksum for private repos
B
Use go mod private
C
Private modules not supported
D
Use different module path
33

Question 33

What is minimal module compatibility?

A
Modules work with newer Go versions than specified
B
Modules require exact Go version match
C
Modules don't specify Go version
D
Backward compatibility only
34

Question 34

How do you debug module issues?

bash
go mod why github.com/some/package
go list -m all
A
go mod why shows why dependency is needed
B
go mod debug
C
Check go.mod manually
D
Cannot debug modules
35

Question 35

What is the most important principle for Go package design?

A
Clear API boundaries, consistent naming, minimal dependencies
B
Maximize exported functions
C
Use global variables
D
Avoid interfaces

QUIZZES IN Golang