You need to close fIn after the copy is done. As it is now, fIn.Close()
happens after io.Copy(os.Stdout, buf), which completes after your cat
command finishes. But cat won't finish until its input pipe returns EOF,
which happens after fIn.Close().

On Wed, Feb 7, 2018 at 5:46 PM Or Rikon <riko...@gmail.com> wrote:

> Hi!
>
> I asked this previously on Stack Overflow but so far have not received a
> response, so I figured I will try here as well.
>
> I'm trying to wrap an executable that takes an input and output file paths
> as arguments, such that it will be possible to provide the input and output
> as stdin and stdout.
>
> I've written a short script in Go that attempts to do this, but for some
> reason which eludes me, it hangs forever.
>
>
> Here is the script:
>
>
> package main
> import (
>     "bytes"
>     "io"
>     "log"
>     "os"
>     "strings"
>     "syscall"
>
>     "golang.org/x/sync/errgroup")
> /*
>     Expected behavior:
>
>     # Terminal 1
>     $ go run main.go
>
>     # Terminal 2
>     $ cat inPipe > outPipe
>
>     The go program is writing to inPipe and reading from outPipe
>
>     Actual behavior: The program stalls
> */
>
> func main() {
>     eg := &errgroup.Group{}
>
>     inPipe := "inPipe"
>     outPipe := "outPipe"
>
>     if err := syscall.Mkfifo(inPipe, 0644); err != nil {
>         log.Fatal(err)
>     }
>     defer os.Remove(inPipe)
>
>     if err := syscall.Mkfifo(outPipe, 0644); err != nil {
>         log.Fatal(err)
>     }
>     defer os.Remove(outPipe)
>
>     fIn, err := os.OpenFile(inPipe, os.O_WRONLY, os.ModeNamedPipe)
>     if err != nil {
>         log.Fatal(err)
>     }
>     defer fIn.Close()
>
>     eg.Go(func() error {
>         _, err := io.Copy(fIn, strings.NewReader("123"))
>         return err
>     })
>
>     fOut, err := os.OpenFile(outPipe, os.O_RDONLY, os.ModeNamedPipe)
>     if err != nil {
>         log.Fatal(err)
>     }
>     defer fOut.Close()
>
>     buf := &bytes.Buffer{}
>
>     eg.Go(func() error {
>         _, err := io.Copy(buf, fOut)
>         return err
>     })
>
>     if err := eg.Wait(); err != nil {
>         log.Fatal(err)
>     }
>
>     if _, err := io.Copy(os.Stdout, buf); err != nil {
>         log.Fatal(err)
>     }}
>
> --
> 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+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

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

Reply via email to