golang,如何执行需要用户输入的命令

yon*_*zhy 6 go

我想从Go执行perforce命令行"p4"来执行登录作业."p4登录"要求用户输入密码.

如何在Go中运行需要用户输入的程序?

以下代码不起作用.

err  = exec.Command(p4cmd, "login").Run()
if err != nil {
    log.Fatal(err)
}
Run Code Online (Sandbox Code Playgroud)

axw*_*axw 6

os/exec.Command文档:

// Stdin specifies the process's standard input. If Stdin is
// nil, the process reads from the null device (os.DevNull).
Stdin io.Reader
Run Code Online (Sandbox Code Playgroud)

在执行之前设置命令的 Stdin 字段。

  • 尽管这个问题已经有七年了,但如果能提供更多背景信息就更好了。 (2认同)

Vid*_*hah 5

// To run any system commands. EX: Cloud Foundry CLI commands: `CF login`
cmd := exec.Command("cf", "login")

// Sets standard output to cmd.stdout writer
cmd.Stdout = os.Stdout

// Sets standard input to cmd.stdin reader
cmd.Stdin = os.Stdin

cmd.Run()
Run Code Online (Sandbox Code Playgroud)