go - How to use a file as stdin when exec a command -
i have simple code:
cmd := exec.command("cat") cmd.stdout = os.stdout stdin, _ := cmd.stdinpipe() file, _ := os.open("file") io.copy(stdin, file) _ = cmd.run() it works. problem cmd.run() never returns. seems after writing file stdin, cat still waits more content.
i'm trying cat <file does, in case cat exit after reading file.
i wonder how achieve in go?
it's because you don't close stdin. add stdin.close() after io.copy. cat terminate.
also, know it's example there absolutely no error checking. assume that's illustration purposes. :-)
Comments
Post a Comment