[英]How to print to stdout while save the stdout to string at the same time in golang?
我想做的是:
cmd := exec.Command(someCommand)
cmd.Stdout = os.Stdout
cmd.Run()
save(os.Stdout)
由於此命令需要很長時間才能執行,因此我想立即在屏幕上打印結果。 所以我不想使用result := cmd.Output() fmt.Print(result)
保存輸出然后打印
使用MultiWriter :
cmd := exec.Command(someCommand)
var buf bytes.Buffer
cmd.Stdout = io.MultiWriter(os.Stdout, &buf)
cmd.Run()
save(buf.Bytes()) // Bytes() returns a []byte containing the stdout from the commmand.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.