执行命令时如何将文件用作stdin

I have this very 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. But the problem is that cmd.Run() never returns. It seems after writing the file to stdin, cat still waits for more content.

I'm trying to do what cat <file does, in which case cat would exit after reading the file.

I wonder how to achieve that in go?

It's because you you don't close the stdin. Add stdin.Close() after io.Copy. cat will then terminate.

Also, I know it's just an example but there is absolutely no error checking. I assume that's just for illustration purposes. :-)