How to print a line to STDERR and STDOUT in Go?
Posted on In QAIn Go, how to print a string as a line to STDOUT? That is, the string and the newline character, nicely?
And similarly, how to print the line to STDERR?
In Go, you can use the ‘Println()’ func from the ‘fmt’ package to print a line to STDOUT:
import("fmt")
fmt.Println("my msg here")
In Go, os.Stderr
is an io.Writer
, so you can use it with any function that accepts an io.Writer
. 2 of the common ways you may use:
import "os"
os.Stderr.WriteString("your message here")
or, throught fmt.Fprintf
:
import "os"
import "fmt"
fmt.Fprintf(os.Stderr, "your message here");
To print directly to STDERR, you don’t even need the “fmt” package; you can use the built-in “print()”.