On Thu, Jul 19, 2012 at 1:55 PM, Matthew Kanwisher <
ma...@errplane.com> wrote:
> Problem is even a read blocks on an STDOUT stream it seems, conceptually I
> want to do something like this
>
> cmd := exec.Command("tail", "-f", "/tmp/matt.txt")
> stdout, err := cmd.StdoutPipe()
> if err != nil {
> log.Fatal(err)
> }
> if err := cmd.Start(); err != nil {
> log.Fatal(err)
> }
>
> err = nil
>
> buff := make([]byte,1024)
> for {
> select {
> case n, _ stdout.Read(buff): This is the line that I don't
> get how to handle, is there a way I can do a blocking call like this on a
> select?
> if n < 1 {
> fmt.Printf(" ")
> break
> } else {
> fmt.Printf("0")
> fmt.Sprintf("found data -%s", n)
> }
> case <- quit:
> return
> }
> }
>
Reading from stdout is kind of odd.
In any case, no. The select statement works on channels. Using a
goroutine to move data from the file to the channel is the way to go.
Do you expect to have many of these goroutines? Do you expect them to
be reading from an input sources that is never closed? If so, you may
need to use a timeout and a quit channel, or something like that. In
typical cases this is not an issue.
Ian