Go Command Line Tools
Go language provides a rich set of command-line tools that are the core of the Go development process. Mastering these tools is essential for Go developers. This chapter will detail the various commands of the Go toolchain and their usage.
🔧 Go Toolchain Overview
Core Commands
📖 Basic Command Details
1. go help - Help Information
View All Commands
View Specific Command Help
Output Example
2. go version - Version Information
Basic Usage
Version Information Format
3. go env - Environment Variables
View All Environment Variables
View Specific Environment Variables
Set Environment Variables
Important Environment Variables
🔨 Compilation Commands
1. go build - Compile Packages
Basic Usage
Common Options
Build Tags
2. go run - Compile and Run
Basic Usage
Example Code
3. go install - Compile and Install
Basic Usage
Difference from go build
🧹 Cleanup Commands
1. go clean - Clean Files
Basic Usage
Cleanup Options Description
📦 Package Management Commands
1. go mod - Module Management
Initialize Module
Module Maintenance
go.mod File Example
2. go get - Get Dependencies
Basic Usage
Version Selection Syntax
🧪 Testing Commands
1. go test - Run Tests
Basic Usage
Test Options
Example Test File
🎨 Code Quality Tools
1. go fmt - Code Formatting
Basic Usage
2. go vet - Static Analysis
Basic Usage
Common Checks
- Unused variables
- Unreachable code
- Printf format string errors
- Method signature errors
- Struct tag format errors
3. go generate - Code Generation
Basic Usage
Generator Directive Example
📚 Documentation Commands
1. go doc - View Documentation
Basic Usage
2. godoc - Documentation Server
Start Local Documentation Server
🔧 Other Useful Commands
1. go list - List Package Information
Basic Usage
2. go tool - Toolchain
View Available Tools
Common Tools
🎯 Practical Example
Create Complete Project Workflow
📋 Command Quick Reference
Daily Development Commands
Module Management Commands
Information Query Commands
🎓 Summary
In this chapter, we learned about Go's command-line tools in detail:
- ✅ Basic Commands: version, env, help and other information queries
- ✅ Compilation Commands: build, run, install and other compilation tools
- ✅ Module Management: mod, get and other dependency management
- ✅ Testing Tools: test, vet, fmt and other quality assurance
- ✅ Documentation Tools: doc, generate and other auxiliary tools
- ✅ Practical Workflow: Complete project development workflow
Mastering these tools is a fundamental skill for becoming a Go developer. It is recommended to use these commands frequently in practice, as practice makes perfect.
Next, let's learn about Go Program Structure to understand the basic components and organization of Go programs.
Tips
It is recommended to record commonly used commands and parameters, or create aliases to improve development efficiency: