In a Go program, you're tasked with implementing a logging system that can log messages to multiple destinations like files, databases, and the console. Which Go feature would you use to achieve this flexibility?
- Use channels to concurrently log messages to different destinations.
- Use goroutines to spawn separate processes for logging to each destination.
- Use interfaces to define a common logging interface and implement destination-specific loggers.
- Use reflection to dynamically determine the log destination.
By using interfaces in Go, you can define a common logging interface with methods like Log(message string). Each log destination, such as file, database, or console, can then implement this interface with its own logging logic. This approach decouples the logging system from specific destinations, allowing for flexibility in adding or modifying log targets without affecting the core logging functionality. Additionally, it facilitates testing and promotes code reuse.
Loading...
Related Quiz
- A Go interface with no methods is called an _______ interface.
- The _______ package in Go provides support for reflection.
- In a Go application, you're building a plugin system where external modules can be dynamically loaded and executed. How would you utilize reflection to manage these plugins?
- What is the significance of the t.Fatal function in testing?
- What is the primary advantage of using a web framework like Gin or Echo in Go development?