Tools Testing
39 articles
Benchmark code
Write benchmark functions with the `Benchmark` prefix and the signature `func BenchmarkXxx(b *testing.B)`, then run them using `go test -bench=.
Build CLI with cobra
Build a Go CLI with Cobra by installing the CLI generator, initializing a project, adding commands, and building the binary.
Build tags in Go
Build tags in Go are comments that conditionally include or exclude source files during compilation based on OS, architecture, or custom constraints.
CLI with viper config
Build a Go CLI by combining Cobra for command parsing with Viper for flexible configuration management.
Create and publish module
Initialize a module, tag a version in git, and push the tag to publish your Go module.
Cross-compile Go for different OS
To cross-compile Go for a different operating system or architecture, simply set the `GOOS` and `GOARCH` environment variables before running `go build`, or pass them directly as flags to the compiler.
Deploy Go to Kubernetes
Deploy Go to Kubernetes by building a Docker image, creating a Deployment manifest, and applying it with kubectl.
Docker image for Go app
Create a multi-stage Dockerfile to build your Go app in a builder stage and run it in a minimal Alpine image.
Environment variables
Set the GODEBUG environment variable with key=value pairs to control Go runtime behavior and maintain backwards compatibility.
Fuzzing in Go
Enable Go fuzzing with the -asan flag and run tests using -test.fuzz to automatically detect memory errors and logic bugs.
Go generate code generation
Go code generation is handled by the `//go:generate` directive, which allows you to define shell commands that run via `go generate` to create source files from templates or data.
go mod tidy vs download
go mod tidy cleans your dependency files, while go mod download only fetches modules to the cache.
Go vet and staticcheck
Run go vet for standard checks and staticcheck for advanced analysis to catch bugs and improve code quality.
Go workspaces
Go workspaces allow managing multiple modules from a single directory using a go.work file for unified builds and tests.
Graceful shutdown
Implement graceful shutdown for GOCACHEPROG by handling the 'close' command and exiting after responding.
Health checks in K8s Go app
Implement a /healthz HTTP endpoint in your Go app and configure Kubernetes probes to monitor container health.
How does go mod work
`go mod` manages Go modules by tracking dependencies in a `go.mod` file and storing resolved versions in a `go.sum` file to ensure reproducible builds.
How to Debug Go Programs with Delve (dlv)
Debug Go programs by running `dlv debug` to attach the debugger, set breakpoints, and step through code execution.
How to Use golangci-lint for Comprehensive Linting
Configure and run golangci-lint with a .golangci.yml file to enforce code quality standards like govet and gofumpt across your Go project.
How to Use go tool cover for Test Coverage
Run go test -cover to generate data and go tool cover -html to view the report.
How to Use go vet for Static Analysis
Run `go vet` on your package to detect suspicious constructs like unreachable code, incorrect format strings, and shadowed variables that the compiler misses.
How to Use staticcheck for Advanced Static Analysis
Install staticcheck via go install and run staticcheck ./... to analyze your Go code for advanced static analysis issues.
How to write unit tests
Create a _test.go file with Test functions and run go test to verify your code logic.
Integration tests
Run integration tests by building with coverage flags, executing with GOCOVERDIR, and analyzing results with go tool covdata.
Manage dependencies
Manage Go dependencies using the go command for version control and govulncheck for security scanning.
Mock interfaces
Mock interfaces in Go by creating a struct that implements the interface methods to control test behavior and isolate dependencies.
Multi-stage Docker build
Multi-stage Docker builds separate the compilation process from the runtime environment to create smaller, more secure container images.
Private modules
Configure private Go modules by setting the GOPRIVATE environment variable to bypass the public proxy for specific import paths.
Profile with pprof
Profile Go programs by capturing CPU or memory data with runtime/pprof and analyzing it using the pprof tool.
Reduce GC pressure
GODEBUG settings control runtime feature compatibility and cannot be used to reduce garbage collection pressure or optimize memory usage.
Reduce memory allocations
Reduce memory allocations by using the arena package to allocate and free memory in bulk, bypassing the garbage collector.
Replace directive
The `godebug` directive in `go.mod` or `go.work` sets default GODEBUG values to control Go runtime behavior and compatibility.
sync Pool for performance
Use sync.Pool to cache and reuse objects, reducing allocation overhead and garbage collection pressure.
Table-driven tests
Table-driven tests use a slice of structs to define multiple test cases and iterate over them in a single function.
Test coverage
Run go test with the -cover flag to measure code coverage and generate a report.
Test HTTP handlers
Test HTTP handlers by using httptest.NewRecorder and httptest.NewRequest to simulate requests and verify responses.
Use trace tool
Control Go runtime behavior and compatibility using GODEBUG environment variables or source file directives.
Using testify
Use testify's assert or require functions to write clear, concise assertions in your Go unit tests.
Vendor dependencies
Run go mod vendor to copy all dependencies from go.mod into a local vendor directory for offline builds.