Essential Go Commands Every Developer Should Master
This guide walks you through installing Go, verifying the environment, initializing modules, managing dependencies, running and building programs, formatting code, cleaning unused packages, testing, and static analysis using core Go command‑line tools.
Go is widely used for backend development due to its low learning curve and fast compilation. Before using any Go tool, ensure the Go compiler is installed and verify the installation with: go version If Go is not present, download the appropriate archive from go.dev/dl and follow the platform‑specific installation steps.
Inspecting the Go environment
Run the following command to display configuration variables such as GOROOT (location of the SDK) and GOPATH (workspace for source code and binaries):
go envCreating a new project
Initialize a module, which creates a go.mod file that records the module path and its dependencies (similar to Maven’s pom.xml or npm’s package.json):
go mod init <module_name>Adding third‑party packages
Fetch a package and automatically add it to go.mod:
go get <package_path>Running and building the code
Execute the program without producing a binary: go run . Compile a production‑ready executable. Replace <binary_name> with the desired output name and <packages> with the package pattern (e.g., ./... for the whole module):
go build -o <binary_name> <packages>Code formatting and dependency cleanup
Automatically format all source files according to the Go style guidelines: go fmt ./... Remove dependencies that are no longer referenced in the source tree and tidy the go.mod file:
go mod tidyTesting and static analysis
Run unit tests for the entire module: go test ./... Perform static analysis to detect suspicious constructs such as mismatched format strings in Printf calls: go vet ./... The go vet tool uses heuristics to surface issues that the compiler may not catch, helping to improve code reliability.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
