被测试端
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
input := bufio.NewScanner(os.Stdin)
fmt.Println("999")
for input.Scan() {
line := input.Text()
// 输入bye时 结束
if line == "exit" {
fmt.Println("结束")
break
}
if line == "888888888888"{
fmt.Println("99999999999999999999")
}
fmt.Println()
// 更新key对应的val 新key对应的val是默认0值
}
}
测试端
package main
import (
"bufio"
"fmt"
"io"
"os/exec"
"strings"
)
//func main() {
// ctx, cancel := context.WithCancel(context.Background())
// cmd := exec.CommandContext(ctx, "F:\\桌面\\go_frame_\\cmd_close\\Test\\AAAA.exe",">>","ssss.txt")
// cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
// cmd.Stdout = os.Stdout
//
// cmd.Start()
//
// cmd.Wait()
// cancel()
// fmt.Println("退出程序中...", cmd.Process.Pid)
//}
func main(){
cmd0 := exec.Command("F:\\桌面\\go_frame_\\cmd_close\\Test\\test.exe")
stdout0 , err := cmd0.StdoutPipe() // 获取命令输出内容
Stdin0 , err := cmd0.StdinPipe()
if err != nil {
fmt.Println(err)
return
}
if err := cmd0.Start(); err != nil { //开始执行命令
fmt.Println(err)
return
}
useBufferIO := false
if !useBufferIO {
//var outputBuf0 bytes.Buffer
for {
tempoutput := make([]byte, 256)
n, err := stdout0.Read(tempoutput)
if err != nil {
if err == io.EOF { //读取到内容的最后位置
break
} else {
fmt.Println(err)
return
}
}
if n > 0 {
if strings.ReplaceAll(string(tempoutput[:n]),"\n","") == "999"{
Stdin0.Write([]byte("888888888888\n"))
}
if strings.ReplaceAll(string(tempoutput[:n]),"\n","") == "99999999999999999999"{
Stdin0.Write([]byte("exit\n"))
}
fmt.Println(string(tempoutput[:n]))
//outputBuf0.Write(tempoutput[:n])
}
}
//fmt.Println(outputBuf0.String())
} else {
outputbuf0 := bufio.NewReader(stdout0)
touput0 , _ , err := outputbuf0.ReadLine()
if err != nil {
return
}
fmt.Println(string(touput0))
}
}