golang,如何执行需要用户输入的命令 [英] golang, how to execute command which requires input from user
本文介绍了golang,如何执行需要用户输入的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从Go执行perforce命令行p4来完成登录工作。 p4登录要求用户输入密码。
以下代码无法运行。
$ b
err = exec.Command(p4cmd,login)。运行()
if err!= nil {
log.Fatal(err)
}
解决方案
从 os / exec.Command 文档:
// Stdin指定进程的标准输入。如果Stdin是
// nil,则该进程从空设备读取(os.DevNull)。
Stdin io.Reader
在执行命令之前设置Stdin字段。
I want to execute perforce command line "p4" from Go to do the login job. "p4 login" require user to input password.
How can I run a program that requires user's input in Go?
The following code doesn't work.
err = exec.Command(p4cmd, "login").Run()
if err != nil {
log.Fatal(err)
}
解决方案
From the os/exec.Command docs:
// Stdin specifies the process's standard input. If Stdin is
// nil, the process reads from the null device (os.DevNull).
Stdin io.Reader
Set the command's Stdin field before executing it.
这篇关于golang,如何执行需要用户输入的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文