Documentation Commands
go version
- Prints the Go version information.go doc -all strings.Replace
- Prints all documentation, including private declarations.go doc -cmd go
- Prints documentation for Go commands.go doc -src fmt.Println
- Prints the source code for a package or symbol.go doc -u
- Updates the local copy of the documentation.godoc -http=:8080
- Starts a web server to browse the documentation.
Module Commands
go mod init example.com/mymodule
- Initializes a new module in the current directory.go mod tidy
- Adds missing and removes unused dependencies.go mod download
- Downloads the dependencies defined in go.mod.go mod vendor
- Copies the dependencies into the vendor directory.go mod verify
- Verifies that the dependencies are correctly downloaded and unmodified.go mod graph
- Prints the module dependency graph.go mod why github.com/pkg/errors
- Explains why a module is needed.go mod edit
- Opens the go.mod file in an editor.go mod replace github.com/pkg/errors@v1.2.3 => github.com/myuser/errors@v0.0.1
- Replaces a module with another module.go mod clean
- Removes unused dependencies.
Build and Run Commands
go vet ./...
- Examines Go source code and reports any suspicious constructs.go build main.go
- Compiles the packages and dependencies in the current directory and generates an executable file.go build ./...
- Compiles the packages and dependencies in the current directory, all packages recursing down and generates an executable file.go build -o myprogram main.go
- Compiles the packages and dependencies and generates a named executable file.go build -race main.go
- Compiles the packages and dependencies with race condition detection enabled.go build -ldflags "-s -w" main.go
- Adds custom flags to the linker.go run main.go
- Compiles and executes the code in a single step.go run -race main.go
- Runs the code with race condition detection enabled.go install example.com/hello
- Compiles and installs the packages and dependencies.go clean
- Removes object files and cached files.
Test Commands
go test ./...
- Runs all the tests associated with the current package.go test -v ./...
- Runs all the tests with verbose output.go test -cover ./...
- Reports the code coverage of the tests.go test -run TestMyFunction
- Runs tests that match the regular expression.go test -timeout 30s ./...
- Sets a timeout for the tests.go test -short ./...
- Skips long-running tests.go test -count=5 ./...
- Runs the tests multiple times.
Release Commands
Note: GOOS
specifies the target operating system (e.g., linux, windows, darwin for macOS), and GOARCH
specifies the target architecture (e.g., amd64, arm64, 386).
go tool dist list
- Lists all available target platforms.GOOS=linux GOARCH=amd64 go build main.go
- Builds for a specific operating system and architecture.GOOS=windows GOARCH=amd64 go build main.go
- Builds for a specific operating system and architecture.GOOS=darwin GOARCH=amd64 go build main.go
- Builds for a specific operating system and architecture.
Help Commands
go help
- Displays a list of available commands or provides help for a specific command.go help <command>
- Provides detailed help information for a specific command.go help modules
- Provides information about using modules to manage dependencies.go help gopath
- Provides information about the GOPATH environment variable.go help environment
- Provides information about Go environment variables.go help packages
- Provides information about Go packages and their organization.go help testflag
- Provides information about the flags that are recognized by the Go testing package.go help buildmode
- Provides information about the available build modes.go help c
- Provides information about how to invoke the C compiler.
Please visit The Go commands official documentation
Thanks for reading!