Go (Golang) Basics — Files, Commands, go.mod, go.sum, go tidy, vendor, download, Compilation & Testing
A complete beginner-friendly guide
Go is a fast, simple, and powerful language created by Google for building server-side, cloud-native, and distributed systems.
This guide gives a full beginner overview of:
✔ Go project structure
✔ Go files and packages
✔ How to run & compile Go programs
✔ How testing works
✔ What go.mod & go.sum are
✔ go mod commands: tidy, vendor, download
✔ Important Go CLI commands
Go Project Structure
A typical small Go project looks like this:
Understanding Go Files
➤ A .go file contains Go code
Every file starts with a package declaration.
Example main.go:
If a file has package main and func main() → it creates an executable program.
Go Packages
A package is simply a folder containing .go files that share the same package name.
Example:
math.go:
Import in main:
Go Modules (go.mod)
Modules are Go’s dependency & versioning system.
Create a module using:
This generates:
go.mod contains:
-
Module path
-
Go version
-
Required dependencies
-
Replace rules (optional)
Example:
go.sum (Dependency Checksums)
go.sum stores cryptographic checksums of all dependencies.
Purpose:
✔ Security — Ensures dependencies weren't modified
✔ Reproducible builds — Ensures same version works everywhere
✔ Integrity verification
Example:
Go verifies these every time modules are downloaded.
Go Commands for Files & Executables
Run a Go program:
Run entire module:
Build a binary:
Build with output name:
Go Tests (_test.go)
Go has a built-in testing library.
Naming rules:
-
File ends with
_test.go -
Function format:
TestXxx(t *testing.T)
Example:
Run tests:
Verbose:
Important go.mod Commands
(These are essential for real projects)
✔ go mod tidy
Cleans & fixes dependencies.
Does:
-
Add missing dependencies based on imports
-
Remove unused dependencies
-
Update go.sum
-
Keep your module graph correct
Command:
Use it after:
-
Adding imports
-
Removing imports
-
Before committing code
✔ go mod vendor
Creates a vendor/ folder with copies of all dependencies.
Purpose:
✔ Offline builds
✔ Reproducible CI builds
✔ Enterprise environments with blocked internet
Command:
To compile using vendor folder:
✔ go mod download
Downloads all dependencies into Go’s module cache:
Command:
Use cases:
-
For CI/CD systems
-
For Docker builds
-
For warming cache
-
When preparing offline builds
You can download specific modules:
✔ go mod verify
Verifies checksums in go.sum.
Ensures no dependency code was tampered with.
go get (Add/Upgrade Dependencies)
Add new dependency:
Upgrade dependency:
Remove dependency (indirectly):
Go Build Modes
Cross-compile for Linux:
Cross-compile for macOS ARM:
Cross-compile for Windows:
Go supports cross-compilation without extra tools.
Summary Table
| Feature / Command | Purpose |
|---|---|
go.mod | Lists module name + dependencies |
go.sum | Stores dependency checksums |
go run | Build + run app |
go build | Compile binary |
go test | Run tests |
go mod tidy | Add missing + remove unused deps |
go mod vendor | Copy dependencies into vendor/ |
go mod download | Download all required deps |
go fmt | Format code |
go get | Add/upgrade dependency |
Final Text Diagram: Go Project Workflow
No comments:
Post a Comment