How can I print to Stderr in Go without using log
How can I write a message to Stderr without using log
?
A comment in this SO post shows how to do it with log
: log.Println("Message")
, but what if I don't want a timestamp?
Is the following good Go?
os.Stderr.WriteString("Message")
If you don't want timestamps, just create a new log.Logger
with flag
set to 0
:
l := log.New(os.Stderr, "", 0)
l.Println("log msg")
EDIT:
Is the following good Go?
os.Stderr.WriteString("Message")
This is acceptable, and you can also use fmt.Fprintf
and friends to get formatted output:
fmt.Fprintf(os.Stderr, "number of foo: %d", nFoo)
Using the fmt
package, you can choose to write to stderr
this way:
import "fmt"
import "os"
func main() {
fmt.Fprintln(os.Stderr, "hello world")
}
The Go builtin functions print
and println
print to stderr. So if you simply want to output some text to stderr you can do
package main
func main() {
println("Hello stderr!")
}
Documentation: https://golang.org/pkg/builtin/#print