To write log messages to the standard output (stdout) in Go, you can use the log
package provided by the Go standard library. Here's an example:
package main
import (
"log"
)
func main() {
// Set flags to include date and time in log messages
log.SetFlags(log.LstdFlags)
// Print log messages to stdout
log.Println("This is a log message.")
log.Printf("This is a formatted log message with values: %s, %d", "foo", 123)
}
In the example above, the log.SetFlags(log.LstdFlags)
line sets the flags to include the date and time in the log messages. You can customize this according to your needs.
You can use log.Println()
to print a simple log message and log.Printf()
to print a formatted log message with values.
When you run the program, the log messages will be printed to the standard output like this:
2021/08/19 14:39:46 This is a log message.
2021/08/19 14:39:46 This is a formatted log message with values: foo, 123
Note that log.Print()
and log.Printf()
write log messages to the standard output (stdout) by default, so you don't need to specify it explicitly.