If your go
program's stdin and stdout is a terminal, and you set your exec.Command
's stdout and stdin to the go
program's stdout and stdin, you can indeed expect python
(in this case) to interact with the terminal just like it was executed directly.
This code:
package main
import(
"os"
"os/exec"
)
func main() {
cmd := exec.Command("python3")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
panic(err)
}
}
Has this behavior:
% go run t.go
Python 3.9.6 (default, Aug 5 2022, 15:21:02)
[Clang 14.0.0 (clang-1400.0.29.102)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello world")
hello world
>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit
>>> ^D
[2023/04/22 11:31:20 CDT ] ~
Your code was missing cmd.Stdin = os.Stdin
.