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 | 6 of 8 | next ›
Discussion Overview
groupgolang-nuts @
postedFeb 11, '15 at 8:41a
activeFeb 12, '15 at 12:40p



site design / logo © 2022 Grokbase