zoukankan      html  css  js  c++  java
  • 1.10 read.wirte from stdin stdout the child process

    // 主进程读取子进程执行的结果
    
    package main
    
    import (
    	"fmt"
    	"os/exec"
    	"runtime"
    )
    
    func main() {
    
    	var cmd string
    
    	if runtime.GOOS == "windows" {
    		cmd = "dir"
    	} else {
    		cmd = "ls"
    	}
    
    	proc := exec.Command(cmd)
    
    	// Output will run the process
    	// terminates and returns the standard
    	// output in a byte slice.
    	buff, err := proc.Output()
    
    	if err != nil {
    		panic(err)
    	}
    
    	// The output of child
    	// process in form
    	// of byte slice
    	// printed as string
    	fmt.Println(string(buff))
    
    }
    
    /*
    b.go
    go_web
    
    */
    
    

    stdout

    // 输出到标准输出
    package main
    
    import (
    	"bytes"
    	"fmt"
    	"os/exec"
    	"runtime"
    )
    
    func main() {
    
    	var cmd string
    
    	if runtime.GOOS == "windows" {
    		cmd = "dir"
    	} else {
    		cmd = "ls"
    	}
    
    	proc := exec.Command(cmd)
    
    	buf := bytes.NewBuffer([]byte{})
    
    	// The buffer which implements
    	// io.Writer interface is assigned to
    	// Stdout of the process
    	proc.Stdout = buf
    
    	// To avoid race conditions
    	// in this example. We wait till
    	// the process exit.
    	proc.Run()
    
    	// The process writes the output to
    	// to buffer and we use the bytes
    	// to print the output.
    	fmt.Println(string(buf.Bytes()))
    
    }
    
    /*
    b.go
    go_web
    */
    

    标准输入到标准输出

    package main
    
    import (
    	"bufio"
    	"fmt"
    	"os"
    )
    
    func main() {
    	sc := bufio.NewScanner(os.Stdin)
    
    	for sc.Scan() {
    		fmt.Println(sc.Text())
    	}
    }
    
    /*
    hhhh
    hhhh
    jjjj
    jjjj
    kklkln,k
    kklkln,k
    
    */
    
    
    package main
    
    import (
    	"bufio"
    	"fmt"
    	"io"
    	"os/exec"
    	"time"
    )
    
    func main() {
    	cmd := []string{"go", "run", "sample.go"}
    
    	// The command line tool
    	// "ping" is executed for
    	// 2 seconds
    	proc := exec.Command(cmd[0], cmd[1], cmd[2])
    
    	// The process input is obtained
    	// in form of io.WriteCloser. The underlying
    	// implementation use the os.Pipe
    	stdin, _ := proc.StdinPipe()
    	defer stdin.Close()
    
    	// For debugging purposes we watch the
    	// output of the executed process
    	stdout, _ := proc.StdoutPipe()
    	defer stdout.Close()
    
    	go func() {
    		s := bufio.NewScanner(stdout)
    		for s.Scan() {
    			fmt.Println("Program says:" + s.Text())
    		}
    	}()
    
    	// Start the process
    	proc.Start()
    
    	// Now the the following lines
    	// are written to child
    	// process standard input
    	fmt.Println("Writing input")
    	io.WriteString(stdin, "Hello
    ")
    	io.WriteString(stdin, "Golang
    ")
    	io.WriteString(stdin, "is awesome
    ")
    
    	time.Sleep(time.Second * 2)
    
    	proc.Process.Kill()
    
    }
    
    
    
  • 相关阅读:
    小程序 ----- 使用less框架
    小程序 ------ 选择器(十)
    小程序 ------ 样式(九)
    小程序 --- 事件绑定(八)
    记一次接口数据获取最大最小值
    flutter——命名路由跳转传值
    Dart——库
    Dart——基础
    人口普查小结
    人口普查-运行截图
  • 原文地址:https://www.cnblogs.com/zrdpy/p/8593305.html
Copyright © 2011-2022 走看看