Skip to main content

How to Print to Standard Error in Go

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A Go program has two output streams: standard output, where fmt.Println writes, and standard error, os.Stderr, for problems. Write a warning with fmt.Fprintln(os.Stderr, ...) and it still reaches the screen when the results go into a file.

Syntax​

Syntax
fmt.Fprintln(os.Stderr, <values>)
PartWhat it does
fmt.FprintlnPrints like fmt.Println, but to the writer you give it.
os.StderrThe standard error stream.
<values>What to print, separated by spaces, followed by a new line.

The fmt documentation: "Fprintln formats using the default formats for its operands and writes to w. Spaces are always added between operands and a newline is appended."

The Example​

The example prints a result to standard output and a warning to standard error, then sends the output to a file to show which line goes where.

Prerequisites​

  • Go. go run compiles and runs a single file. This guide uses Go 1.27.4.

Step 1: Write the Code​

Create a file named main.go:

main.go, which prints Report saved. with fmt.Println and Warning: 2 rows were skipped. with fmt.Fprintln to os.Stderr.

main.go
package main

import (
"fmt"
"os"
)

func main() {
fmt.Println("Report saved.")
fmt.Fprintln(os.Stderr, "Warning: 2 rows were skipped.")
}

Step 2: Run It, Then Redirect the Output​

Run it, then send standard output to a file with > and look inside the file:

Terminal
go run main.go
go run main.go > report.txt
cat report.txt
Output
Report saved.
Warning: 2 rows were skipped.
Warning: 2 rows were skipped.
Report saved.

A zsh terminal where go run main.go prints both lines, go run main.go > report.txt prints only the warning, and cat report.txt shows Report saved.

The first run shows both lines. In the second, > sent standard output into report.txt, so only the warning stayed on screen; cat then shows the report line inside the file. In PowerShell 7.6 the same commands behave the same way: > redirects only standard output, and on Windows cat is an alias of Get-Content.

Common Mistakes​

MistakeWhat happensDo this instead
Printing warnings with fmt.PrintlnThey end up in the redirected file, mixed with the resultsPrint them to os.Stderr
Printing results to os.StderrThey don't reach the file or the next program in a pipeKeep results on standard output
Expecting the message to stop the programIt only prints; the code carries onReturn or call os.Exit when the program can't continue

Troubleshooting​

SymptomLikely causeFix
Error messages are missing from a log fileOnly standard output was redirectedAdd 2> errors.txt to save standard error too
A warning appears inside saved dataIt was printed with fmt.PrintlnSwitch it to fmt.Fprintln(os.Stderr, ...)

FAQ​

Is there a package for this?​

Yes: the log package writes to standard error by default and adds a date and time to each line.

How do I save standard error to a file?​

Redirect it with 2>, as in go run main.go 2> errors.txt. It works the same in zsh, bash and PowerShell.

Conclusion​

fmt.Println writes results to standard output and fmt.Fprintln(os.Stderr, ...) writes problems to standard error. Redirect the results and the warnings still show up on screen.

Additional Resources​

  • fmt.Fprintln, in the Go standard library documentation.
  • os.Stderr, in the Go standard library documentation.