Whoops, I take that back. I forgot that you aren't allowed to use Run with
StdoutPipe (or StderrPipe).

But one thing: you should *always* check the return value from the process
anyway, even if the pipe from StdoutPipe lets you read something from it.
What if your process starts and runs find for a few seconds but then fails
due to a disk being randomly unmounted or some other random IO error? If
you just trust that the end of the pipe is the end of data you might miss
something at the end.

Try something like this:

Start the process
Read from stdout until you get EOF. This will always happen when the
process exits (I believe)
Check the result from Wait: if nil, everything was ok. Otherwise the
process exited for some bad reason and either some or no data was processed
from it.

You might want to do something different if some versus no data was read,
but in general I would consider both the same sort of error to be reported.

On Wednesday, February 11, 2015 at 10:21:09 PM UTC-5, Kevin Malachowski
Just like you realized, there are multiple things you need to do: read the
output and check the exit status, each of which are blocking operations.
Using goroutines to delegate these tasks simultaneously is an elegant way
to handle it: http://play.golang.org/p/izD1rjVtZ_
On Wednesday, February 11, 2015 at 3:41:25 AM UTC-5, Clint O wrote:


I should properly explain what I am trying to accomplish. Perhaps I'm
approaching this the wrong way. I need to do the equivalent of a
popen("/bin/zcat filename"). The file is likely to be fairly large, and I
will usually only need to examine the first 10-20 lines of the file and
then I can shut down the command. I'm essentially just examining the
comment block at the top of the file.

What I'm interested in catching is if zcat runs into an error due to a
missing file. When I use:

package main

import (

func main() {

cmd := exec.Command("/bin/zcat", "foo.gz")

stdout, err1 := cmd.StdoutPipe()

if err1 = cmd.Start(); err1 != nil {

If foo.gz doesn't exist, err1 is not populated with a bad exit status as
I would have expected. The code ultimately gets down to when it's starting
to try reading from the pipe and gets "reading file: EOF".

I can attempt to use StderrPipe() as well to get the details from the
failed call to zcat, but reading from that will block if nothing is writing
to it, so that's not right, either.

Is the only way to safely do this to test for the existence of "foo.gz"
before I try running this snippet?

I know it seems like I'm being pedantic about this, but I am just trying
to understand the failure cases, and I would like the error messages to be
at least well-formed so that an end-user can figure out what's wrong.



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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts


Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 7 of 8 | next ›
Discussion Overview
groupgolang-nuts @
postedFeb 11, '15 at 8:41a
activeFeb 12, '15 at 12:40p



site design / logo © 2022 Grokbase