第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Golang exec.Command - 對(duì)話框

Golang exec.Command - 對(duì)話框

Go
qq_笑_17 2023-06-01 18:03:11
Go 程序運(yùn)行帶參數(shù)的外部 soft.exe:cmd := exec.Command("soft.exe", "-text") out, _ := cmd.CombinedOutput() fmt.Printf("%s", out)soft.exe 文件有一些輸出和等待輸入值,例如:請(qǐng)選擇代碼:1、2、3、4在 shell 窗口中以通常的方式,我只需鍵入“1”并按 Enter,soft.exe 就會(huì)給我結(jié)果。謝謝,您的代碼是 [some number]如何在運(yùn)行后填充“1”并使用 GoLang 獲取輸出?在我的示例中,運(yùn)行 soft.exe 后它會(huì)立即完成“請(qǐng)選擇代碼:1、2、3、4”的工作。
查看完整描述

1 回答

?
阿晨1998

TA貢獻(xiàn)2037條經(jīng)驗(yàn) 獲得超6個(gè)贊

您需要將 os.Stdin 重定向到 cmd.Stdin,將 os.Stdout 重定向到 cmd.Stdout

在 godoc 中查看: https: //golang.org/pkg/os/exec/#Cmd

   // Stdin specifies the process's standard input.
   //
   // If Stdin is nil, the process reads from the null device (os.DevNull).
   //
   // If Stdin is an *os.File, the process's standard input is connected
   // directly to that file.
   //
   // Otherwise, during the execution of the command a separate
   // goroutine reads from Stdin and delivers that data to the command
   // over a pipe. In this case, Wait does not complete until the goroutine
   // stops copying, either because it has reached the end of Stdin
   // (EOF or a read error) or because writing to the pipe returned an error.
   Stdin io.Reader

此示例在 Windows 上進(jìn)行了測(cè)試。

package main


import (

    "fmt"

    "os"

    "os/exec"

)


func main() {

    cmd := exec.Command("yo")

    cmd.Stderr = os.Stderr

    cmd.Stdout = os.Stdout

    cmd.Stdin = os.Stdin

    if err := cmd.Run(); err != nil {

        fmt.Println(err.Error())

        os.Exit(1)

    }


}


查看完整回答
反對(duì) 回復(fù) 2023-06-01
  • 1 回答
  • 0 關(guān)注
  • 210 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)