How do I print message to stderr in Go?
There are multiple methods to send a message to stderr
:
Creating a new
log.Logger
:l := log.New(os.Stderr, "", 1) l.Println("log message")
Using
fmt.Fprintf
:fmt.Fprintf(os.Stderr, "log message: %s", str)
Directly writing to
os.Stderr
usingos.Stderr.WriteString
:os.Stderr.WriteString("log message")
The log
package by default prints to os.Stderr
.
You can also use os.Stderr
directly (it's an os.File
).