io.Copy(/dev/null, equivalent

2,834 views
Skip to first unread message

gocss

unread,
Dec 24, 2015, 11:57:14 AM12/24/15
to golang-nuts

like to dump stderr to bit bucket
io.Copy(/dev/null, stderr)

what is the equivalent for /dev/null in golang ?
or do I have write something to eat it ?

css

Brad Fitzpatrick

unread,
Dec 24, 2015, 12:00:53 PM12/24/15
to gocss, golang-nuts
io.Copy(ioutil.Discard, yourReader)


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Konstantin Khomoutov

unread,
Dec 24, 2015, 12:07:57 PM12/24/15
to gocss, golang-nuts
import "io/ioutil"
io.Copy(ioutil.Discard, stderr)

Dave Cheney

unread,
Dec 24, 2015, 12:13:10 PM12/24/15
to golang-nuts
Stderr is where your program writes data to, so unless you are writing to stderr there is nothing to discard.

Are you using os/exec to call a sub process and wishing to discard that child's output?

Rob Pike

unread,
Dec 24, 2015, 1:34:39 PM12/24/15
to Dave Cheney, golang-nuts
For the literal question being asked, there's always

os.Stderr = ioutil.Discard

but output to standard error includes important error messages, so this would make it much harder to understand what's happening if the program misbehaves.

In other words, here's how to do it but don't do it.

-rob


On Thu, Dec 24, 2015 at 9:13 AM, Dave Cheney <da...@cheney.net> wrote:
Stderr is where your program writes data to, so unless you are writing to stderr there is nothing to discard.

Are you using os/exec to call a sub process and wishing to discard that child's output?

Brad Fitzpatrick

unread,
Dec 24, 2015, 2:11:37 PM12/24/15
to Rob Pike, Dave Cheney, golang-nuts
On Thu, Dec 24, 2015 at 10:33 AM, Rob Pike <r...@golang.org> wrote:
For the literal question being asked, there's always

os.Stderr = ioutil.Discard

cannot use ioutil.Discard (type io.Writer) as type *os.File in assignment: need type assertion

Forget about your https://golang.org/issue/13473 already? :)

Rob Pike

unread,
Dec 24, 2015, 4:59:28 PM12/24/15
to Brad Fitzpatrick, Dave Cheney, golang-nuts
I did indeed. I wished so hard for it to be fixed I'm already using it in my head.

-rob

gocss

unread,
Dec 24, 2015, 5:34:26 PM12/24/15
to golang-nuts
yes calling a sub process and wishing to discard that child's stderr output [at least initially for testing]

Dave Cheney

unread,
Dec 24, 2015, 9:46:00 PM12/24/15
to golang-nuts
If cmd.Stderr is nil, the output will be discarded. This is the default behaviour so you shouldn't have to do anything.
Reply all
Reply to author
Forward
0 new messages