dtc66318 2019-09-04 02:33
浏览 142
已采纳

如何在Golang中使用os / exec处理用户输入? 我无法停止输入阶段

First, I build a command as exec.exe from this:

package main
import "fmt"
func main() {
    var input string
    fmt.Println("input a value")
    fmt.Scanln(&input)
    fmt.Println(input)

    fmt.Println("input another value")
    fmt.Scanln(&input)
    fmt.Println(input)
}

Then I want to use os/exec pacage to run it:

package main

import (
    "fmt"
    "os/exec"
)

func main() {
    cmd := exec.Command("G:\\go_workspace\\GOPATH\\src\\pjx\\modules\\exec\\exec")

    stdin, e := cmd.StdinPipe()
    if e != nil {
        panic(e)
    }
    stdout, e := cmd.StdoutPipe()
    if e != nil {
        panic(e)
    }
    if e:=cmd.Start();e!=nil {
        panic(e)
    }
    stdin.Write([]byte("hello"))
    var buf = make([]byte, 512)
    n, e := stdout.Read(buf)
    if e != nil {
        panic(e)
    }
    fmt.Println(string(buf[:n]))

    if e := cmd.Wait(); e != nil {
        panic(e)
    }
}

Finally I run it, and result will pause on the user input stage,like:

(in case picture is not loaded, they're paused on input stage)

please input a value:

1
12
232

unexpected result, pausing on user input stage

Did I use cmd pipe in a wrong way?

  • 写回答

3条回答 默认 最新

  • duanganleng0577 2019-09-04 04:17
    关注

    The program is blocking because the fmt.Scanln in the subprocess is waiting for the character (an EOF would also cause it to return). To avoid blocking, your input should include two s, or you can just call 'stdin.Close()' to indicate that the input stream is done.

    And since the subprocess calls Scanln and Println multiple times, a single call to stdout.Read may not read the complete output from the subprocess. You can keep calling the stdout.Read() until an io.EOF error is returned, or just use the ioutil.ReadAll.

    func main() {
        cmd := exec.Command("G:\\go_workspace\\GOPATH\\src\\pjx\\modules\\exec\\exec")
    
        stdin, e := cmd.StdinPipe()
        if e != nil {
            panic(e)
        }
    
        stdout, e := cmd.StdoutPipe()
        if e != nil {
            panic(e)
        }
        if e := cmd.Start(); e != nil {
            panic(e)
        }
        _, e = stdin.Write([]byte("hello
    world
    "))
        if e != nil {
            panic(e)
        }
        stdin.Close()
    
        out, _ := ioutil.ReadAll(stdout)
        // or you can use a loop
        //for {
        //  var buf = make([]byte, 512)
        //  n, e := stdout.Read(buf)
        //  if e == io.EOF {
        //      break
        //  }
        //  if e != nil {
        //      panic(e)
        //  }
        //  fmt.Println(string(buf[:n]))
        //}
    
        fmt.Println(string(out))
    
        if e := cmd.Wait(); e != nil {
            panic(e)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错