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

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -