This repository contains a collection of useful Go tools and utility packages designed to be easily integrated into your projects.
To use any tool from this repository, you'll need to import it into your Go project and then run go mod tidy
.
The stoper
package provides a simple way to handle graceful application shutdown upon receiving OS signals like Ctrl+C
(SIGINT) or SIGTERM
. It ensures your application can perform clean-up actions before exiting.
To include stoper
in your project, simply import it:
import (
"fmt"
// ... other imports
"github.com/cooler-SAI/go-Tools/stoper" // Import the stoper package
)
The zerolog
package provides a fast and flexible logger that produces structured (JSON by default) and leveled logs. It's designed for high performance and offers a simple API for rich logging capabilities.
To include zerolog
in your project, you'll first need to get the package:
import (
"fmt"
// ... other imports
"github.com/cooler-SAI/go-Tools/zerolog" // Import the zerolog package
)
Just use this code in your project:
// Using the logger with default settings (text output)
zerolog.Log.Info().Msg("This is a message with default settings")
// Initialize with colored console output
zerolog.Init()
// Now logs will be in colored format
zerolog.Log.Info().Msg("This is an informational message")
zerolog.Log.Warn().Msg("This is a warning")
zerolog.Log.Error().Msg("This is an error")
This repository now includes a Dockerfile
located in the docker/
directory. This Dockerfile provides a standardized way to build a minimal and secure Docker image for Go applications that might utilize the tools from this repository.
- Multi-stage Builds: Utilizes a builder stage for compilation and a separate, minimal runtime stage (
alpine
based) to keep the final image size small. - Security Focused: Creates a non-root user (
appuser
) to run the application, enhancing security. - Optimized for Go:
- Disables CGO by default (
CGO_ENABLED=0
) for static binaries. - Targets Linux (
GOOS=linux
). - Strips debugging symbols (
-ldflags="-s -w"
) to reduce binary size. - Dependency Caching: Leverages Docker's layer caching for Go module dependencies to speed up subsequent builds.
- Configurable: Designed to build a Go project located in the parent directory relative to the
docker/
directory (i.e., your main Go project should be one level up from where theDockerfile
resides).
To build a Docker image for your Go project using this Dockerfile:
- Ensure your Go project is structured such that the
docker/
directory (containing theDockerfile
) is a subdirectory of your main project. - Navigate to the
docker/
directory in your terminal. - Run the Docker build command or use config in your IDE
Utility functions for generating random values using math/rand/v2.
To include random
in your project, you'll first need to get the package:
import (
"fmt"
// ... other imports
"github.com/cooler-SAI/go-Tools/random" // Import the zerolog package
)
To use any tool from this repository, you'll need to import it into your Go project and then run go mod tidy
.
// Returns random int in [min, max] range
num := random.RandRange(1, 100)
This section describes how to configure GoLand IDE to automatically compile, run, and then delete your Go project's executable file after it finishes execution. This is achieved by using a custom BAT file that manages the entire build and cleanup process.
- Automated Build & Run: Automatically compiles your Go project and launches the executable with a single action (Shift + F10).
- Post-Execution Cleanup: Ensures the compiled executable (.exe on Windows) is deleted immediately after the program finishes, keeping your project directory clean.
- Simplified Workflow: Eliminates the need for manual cleanup commands after each test or run.
- IDE Integrated: Fully configured within GoLand's Run/Debug configurations for seamless developer experience.
- Customizable: The underlying BAT script can be easily modified to include additional pre-run or post-run commands if needed.
Important Notes for the BAT file:
- PROJECT_DIR: Set this to the absolute path of the directory containing your main Go file (e.g., transactions.go).
- BUILD_NAME: Specify your desired executable file name (e.g., transactions.exe).
- go build -o "%BUILD_NAME%" transactions.go: This command compiles your transactions.go and saves the result as transactions.exe within the PROJECT_DIR.
Now, you need to configure GoLand to use this BAT file to control the build and run process.
-
Open Run/Debug Configurations: Go to Run | Edit Configurations... in the top menu of GoLand.
-
Select or Create a Configuration: Choose your existing "Go Build" or "Go Application" configuration for your project (e.g., postgres or transactions). If you don't have one, create a new one.
-
Configure the "Before launch" section:
REMOVE the default "Build" (Go Build) step: In the "Before launch" list, find the step of type "Build" (Go Build). Select it and click the minus (-) button to remove it. This is crucial, as the build will now be handled by your BAT file.
- Add your "External Tool": Click the plus (+) button and select Run External Tool. If you haven't configured this tool before, click + again to create a new one. Fill in the fields for the new external tool as follows:
- Name: Run and Clean Build (or any descriptive name)
- Program: Provide the full, absolute path to your build_and_run.bat file.
- Example: D:\Enterprise Development\Go-projects\go-projects\base\postgres\build_and_run.bat
- Arguments: Leave this field BLANK. The BAT file handles the paths internally and does not require arguments from GoLand.
- Working directory:
$ProjectFileDir$ (This macro points to the root directory of your Go project, e.g., D:\Enterprise Development\Go-projects\go-projects\base).
- Apply Changes: Click OK to save the new external tool. Ensure that Run and Clean Build is now the only step listed in the "Before launch" section (apart from potentially "Build dependencies" or similar steps GoLand adds by default). Click Apply, then OK in the "Run/Debug Configurations" window to save all changes.
-
Once the GoLand configuration is set up:
-
Simply run your project using Shift + F10 (or by clicking the "Run" button in GoLand).
-
The build_and_run.bat script will automatically compile your Go application, execute it, and then delete the generated executable file upon completion.
"D:\Enterprise Development\Go-projects\go-projects\base\postgres\build_and_run.bat"
--- Build Process ---
Current working directory for build: D:\Enterprise Development\Go-projects\go-projects\base\postgres
Target build path: D:\Enterprise Development\Go-projects\go-projects\base\postgres\transactions.exe
Go build successful.
--- Launching Program ---
Launching: "D:\Enterprise Development\Go-projects\go-projects\base\postgres\transactions.exe"
Program finished with exit code: 0
--- Cleanup Process ---
Deleting build: "D:\Enterprise Development\Go-projects\go-projects\base\postgres\transactions.exe"
Build successfully deleted.
Process finished with exit code 0
go test -bench=Benchmark -benchmem -run=^$ -v
This section provides a set of benchmark tests to evaluate the performance of various operations in Go, including string concatenation, integer addition, and slice appending. The benchmarks measure both execution time and memory allocations. Using pprof, you can analyze CPU and memory usage to identify performance bottlenecks.
- For Web Testing:
# 1. Replace functions in pprof-web-template.go with your own
# 2. Run:
go run pprof-web-template.go
# 3. Open in browser:
# - http://localhost:8080/ - instructions
# - http://localhost:8080/debug/pprof/ - profiling
- For Local Testing:
# 1. Replace functions in pprof-local-template.go with your own
# 2. Run:
go run pprof-local-template.go
# 3. Analyze created .pprof files:
go tool pprof cpu.pprof
go tool pprof memory.pprof
- For Running Tests:
go test -v -cpuprofile=cpu.test.pprof -memprofile=memory.test.pprof