modules Flashcards

(10 cards)

1
Q

What is a Go Module?

A

A collection of related Go packages with versioned dependencies

Key Files:
* go.mod (declares module path and dependencies)
* go.sum (records cryptographic checksums)

module github.com/yourname/project

go 1.21

require (
    github.com/pkg/errors v0.9.1
    golang.org/x/sync v0.3.0
)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Initializing a Module

A
go mod init github.com/yourname/project

What it does:
Creates a go.mod file in your project root.
Rule:
Module path should match your code’s import path.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Adding Dependencies

A

Automatically:
Just import in code and run:

go mod tidy

Manually:

go get github.com/pkg/errors@v0.9.1

Key Flags:
@latest - gets newest version
@v1.2.3 - specific version

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

go mod tidy

A

Purpose:

Adds missing dependencies

Removes unused dependencies

Updates go.mod/go.sum
Always run this:
After changing imports or before committing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Upgrading/Downgrading

A

Check versions:
go list -m -versions github.com/pkg/errors

Upgrade:
go get github.com/pkg/errors@latest

Downgrade:
go get github.com/pkg/errors@v0.8.1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Vendor Directory

A

Create vendor/:
go mod vendor

Use vendor/:
go build -mod=vendor

Purpose:
Stores dependency copies locally for reproducibility.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Version Selection

A

How Go chooses versions:
1. Highest compatible semver tag (e.g., v2.3.4)
2. Pseudo-versions (e.g., v0.0.0-20231012000000-abc123)
Key Terms:
* Minimal version selection (MVS)
* Semantic Import Versioning (major versions ≥2 change import paths)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Replacing Dependencies

A

In go.mod:

replace ( github.com/old/mod => ../local/mod golang.org/x/net => github.com/golang/net v0.7.0 )

Use Cases:
* Local development
* Forked dependencies

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Module Proxy

A

Default Proxy:
proxy.golang.org (Google-run)
Environment Variables:

GOPROXY=https://proxy.golang.org,direct

GOPRIVATE=github.com/yourcompany/* (skip proxy for private repos)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Key Commands Cheat Sheet

A

Command Purpose
go mod init Initialize new module
go mod tidy Sync dependencies
go get -u Update dependencies
go list -m all View all dependencies
go mod why Explain why a dependency exists
go mod graph Print dependency graph
Golden Rule:
Always commit both go.mod and go.sum to version control!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly