This repository demonstrates the implementation of the Singleton pattern in Go. The project focuses on creating a globally accessible configuration manager (ConfigManager
) that maintains a single instance throughout the application's lifecycle. This ensures that configuration settings are managed centrally and efficiently, minimizing resource use and ensuring consistency.
The Singleton Pattern ensures that a class has only one instance and provides a global point of access to it. This is particularly useful in managing shared resources such as configuration settings, where a single point of control is necessary to manage state across an application. In this project, the ConfigManager
class is designed as a Singleton to handle configuration settings, making it easy to manage and update settings dynamically.
- cmd/: Contains the application entry point (
main.go
), demonstrating the use of the Singleton pattern with theConfigManager
. - pkg/
- singleton/: Implements the
ConfigManager
, the Singleton class managing the application's configuration.
- singleton/: Implements the
- internal/
- database/: Contains the simulated
Connection
class used for demonstrating the Singleton's use in managing database connections.
- database/: Contains the simulated
Environment variables are used to initialize the settings in the ConfigManager
. The following variables can be set:
DATABASE_CONNECTION_STRING
: Database connection string.API_KEY
: API key for external services.LOG_LEVEL
: Logging level for the application.
- Environment Variable Support: Allows initial configuration through environment variables.
- Dynamic Setting Updates: Supports updating settings at runtime through the Singleton interface.
- Detailed Logging: Logs all interactions with the configuration settings for auditing and debugging purposes.
Ensure you have Go installed on your system. You can download it from Go's official site.
Clone this repository to your local machine:
git clone https://github.com/arthurfp/Go_Singleton_Pattern.git
cd Go_Singleton_Pattern
To run the application, execute:
go run cmd/main.go
To execute the tests and verify the functionality:
go test ./...
Contributions are welcome! Please feel free to submit pull requests or open issues to discuss proposed changes or enhancements.
Arthur Ferreira - github.com/arthurfp