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.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *