In Go, safely logging messages from concurrent Goroutines can be achieved using channels to serialize access to the log message and a separate Goroutine to perform the actual logging. Here's an example:
logChan := make(chan string)
go func() {
for msg := range logChan {
fmt.Println(msg) // or write to a file, database, etc.
}
}()
func logMessage(msg string) {
logChan <- msg
}
logMessage
function in your Goroutines to safely log messagesgo func() {
// ...
logMessage("Message to log")
// ...
}()
By using a channel for logging messages, Goroutines can send log messages safely without interfering with each other. The separate logging Goroutine then consumes the log messages from the channel and performs the actual logging operation.
Note that you can modify the logging Goroutine to log to different targets, such as a file or a database, by replacing the fmt.Println
statement with the appropriate operation.
Additionally, to gracefully stop the logging Goroutine, you should close the logChan
when you no longer need to log messages.
close(logChan)
This will cause the logging Goroutine to exit the loop and terminate.