How to print a line to STDERR and STDOUT in Go?

In 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?

asked Jun 5, 2017 by dtivl (2,500 points)

1 Answer

In Go, you can use the 'Println()' func from the 'fmt' package to print a line to STDOUT:


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");
answered Jun 5, 2017 by dtivl (2,500 points)

Please log in or register to answer this question.

Related questions

Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost