go语言将cmd stdout和stderr作为字符串返回而不是打印到控制台
1、直接打印到控制台
从 golang 应用程序中执行 bash 命令,现在 stdout 和 stderr 直接进入控制台:
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
package main
import (
"fmt"
"log"
"os"
"os/exec"
"time"
)
func main() {
ok, outString, errString := runBashCommandAndKillIfTooSlow("dir", 2000)
fmt.Println("ok")
fmt.Println(ok)
fmt.Println("outString")
fmt.Println(outString)
fmt.Println("errString")
fmt.Println(errString)
}
/*
run bash command and kill it if it works longer than "killInMilliSeconds" milliseconds
*/
func runBashCommandAndKillIfTooSlow(command string, killInMilliSeconds time.Duration) (okResult bool, stdout, stderr string) {
fmt.Println("running bash command...")
fmt.Println(command)
// Linux
// cmd := exec.Command("sh", "-c", command)
// Windows
cmd := exec.Command("cmd", "/C", command)
cmd.Stdout = os.Stdout // cmd.Stdout -> stdout
cmd.Stderr = os.Stderr // cmd.Stderr -> stderr
okResult = true
err := cmd.Start()
log.Printf("Waiting for command to finish...")
done := make(chan error, 1)
go func() {
done <- cmd.Wait()
}()
select {
case <-time.After(killInMilliSeconds * time.Millisecond):
if err := cmd.Process.Kill(); err != nil {
log.Fatal("failed to kill: ", err)
okResult = false
}
// allow goroutine to exit
<-done
log.Println("process killed")
case err := <-done:
if err != nil {
log.Printf("process done with error = %v", err)
okResult = false
}
}
if err != nil {
log.Fatal(err)
okResult = false
}
return
}
如果 bash 命令太慢( killInMilliSeconds 参数),程序应该保持其终止 bash 命令的能力。
希望 stdout 和 stderr 作为字符串变量从 runBashCommandAndKillIfTooSlow 函数返回,而不立即打印到控
制台,如何实现。
2、不打印到控制台
将输出设置为 strings.Builder 或 bytes.Buffer:文章来源:https://www.toymoban.com/news/detail-819052.html
var outbuf, errbuf strings.Builder // or bytes.Buffer
cmd.Stdout = &outbuf
cmd.Stderr = &errbuf
运行命令后,您可以通过调用 Builder.String() 获取字符串形式的标准输出和标准错误:文章来源地址https://www.toymoban.com/news/detail-819052.html
stdout := outbuf.String()
stderr := errbuf.String()
package main
import (
"fmt"
"log"
"os/exec"
"strings"
"time"
)
func main() {
ok, outString, errString := runBashCommandAndKillIfTooSlow("dir", 2000)
fmt.Println("ok")
fmt.Println(ok)
fmt.Println("outString")
fmt.Println(outString)
fmt.Println("errString")
fmt.Println(errString)
}
/*
run bash command and kill it if it works longer than "killInMilliSeconds" milliseconds
*/
func runBashCommandAndKillIfTooSlow(command string, killInMilliSeconds time.Duration) (okResult bool, stdout, stderr string) {
fmt.Println("running bash command...")
fmt.Println(command)
// Linux
// cmd := exec.Command("sh", "-c", command)
// Windows
cmd := exec.Command("cmd", "/C", command)
var outBuf, errBuf strings.Builder // or bytes.Buffer
cmd.Stdout = &outBuf
cmd.Stderr = &errBuf
okResult = true
err := cmd.Start()
log.Printf("Waiting for command to finish...")
done := make(chan error, 1)
go func() {
done <- cmd.Wait()
}()
select {
case <-time.After(killInMilliSeconds * time.Millisecond):
if err := cmd.Process.Kill(); err != nil {
log.Fatal("failed to kill: ", err)
okResult = false
}
// allow goroutine to exit
<-done
log.Println("process killed")
case err := <-done:
if err != nil {
log.Printf("process done with error = %v", err)
okResult = false
}
}
if err != nil {
log.Fatal(err)
okResult = false
}
stdout = outBuf.String()
stderr = errBuf.String()
return
}
到了这里,关于go语言将cmd stdout和stderr作为字符串返回而不是打印到控制台的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!