Home > Article > Backend Development > How to Print Messages to Standard Error in Go?
Printing Message to Standard Error in Go
If you need to print debugging or testing logs separately from existing logs, you may consider sending messages to the standard error stream (stderr). This is useful when you want to isolate your logs for easier analysis.
Methods for Printing to stderr
There are multiple ways to print messages to stderr in Go:
1. Using log.Logger:
Create a new log.Logger, specifying os.Stderr as the output stream:
<code class="go">l := log.New(os.Stderr, "", 1) l.Println("log message")</code>
2. Using fmt.Fprintf:
Use fmt.Fprintf to write formatted messages to stderr:
<code class="go">fmt.Fprintf(os.Stderr, "log message: %s", str)</code>
3. Writing Directly to os.Stderr:
Write directly to os.Stderr using os.Stderr.WriteString:
<code class="go">os.Stderr.WriteString("log message")</code>
By directing your logs to stderr, you can easily separate them from other logs and focus on debugging and testing information.
The above is the detailed content of How to Print Messages to Standard Error in Go?. For more information, please follow other related articles on the PHP Chinese website!