golang 學習筆記 使用cmd


package main

import (
	"bytes"
	"fmt"

	"os/exec"
)

func main() {
	cmd0 := exec.Command("go", "env")
	var outputBuf1 bytes.Buffer
	cmd0.Stdout = &outputBuf1
	if err := cmd0.Start(); err != nil {
		fmt.Printf("Error: The first command can not be startup %s\n", err)
		return
	}
	if err := cmd0.Wait(); err != nil {
		fmt.Printf("Error: Couldn't wait for the second command: %s\n", err)
		return
	}
	fmt.Printf("%s\n", outputBuf1.Bytes())

}

  

 

package main

import "fmt"
import "os/exec"

func main() {
        //create cmd
        cmd_go_env := exec.Command("go", "env")
        //cmd_grep:=exec.Command("grep","GOROOT")

        stdout_env, env_error := cmd_go_env.StdoutPipe()
        if env_error != nil {
                fmt.Println("Error happened about standard output pipe ", env_error)
                return
        }

        //env_error := cmd_go_env.Start()
        if env_error := cmd_go_env.Start(); env_error != nil {
                fmt.Println("Error happened in execution ", env_error)
                return
        }

        a1 := make([]byte, 1024)
        n, err := stdout_env.Read(a1)
        if err != nil {
                fmt.Println("Error happened in reading from stdout", err)
        }

        fmt.Printf("Standard output of go env command: %s", a1[:n])
}

  

管道連接

通過調用exec.Start啟動一個進程,通過StdoutPipe將此調用的輸出管道也創建了出來,在這里,我們讀取了此輸出的信息,確實是go env命令的標准輸出,接下來要做的事情就是將此輸出的管道與grep命令的進程進行連接了。我們將上面的代碼進一步充實:

package main

import "fmt"
import "os/exec"
import "bufio"
import "bytes"

func main() {
        //create cmd
        cmd_go_env := exec.Command("go", "env")
        cmd_grep := exec.Command("grep", "GOROOT")

        stdout_env, env_error := cmd_go_env.StdoutPipe()
        if env_error != nil {
                fmt.Println("Error happened about standard output pipe ", env_error)
                return
        }

        //env_error := cmd_go_env.Start()
        if env_error := cmd_go_env.Start(); env_error != nil {
                fmt.Println("Error happened in execution ", env_error)
                return
        }
        /*
                a1 := make([]byte, 1024)
                n, err := stdout_env.Read(a1)
                if err != nil {
                        fmt.Println("Error happened in reading from stdout", err)
                        return
                }

                fmt.Printf("Standard output of go env command: %s", a1[:n])
        */
        //get the output of go env
        stdout_buf_grep := bufio.NewReader(stdout_env)

        //create input pipe for grep command
        stdin_grep, grep_error := cmd_grep.StdinPipe()
        if grep_error != nil {
                fmt.Println("Error happened about standard input pipe ", grep_error)
                return
        }

        //connect the two pipes together
        stdout_buf_grep.WriteTo(stdin_grep)

        //set buffer for reading
        var buf_result bytes.Buffer
        cmd_grep.Stdout = &buf_result

        //grep_error := cmd_grep.Start()
        if grep_error := cmd_grep.Start(); grep_error != nil {
                fmt.Println("Error happened in execution ", grep_error)
                return
        }

        err := stdin_grep.Close()
        if err != nil {
                fmt.Println("Error happened in closing pipe", err)
                return
        }

        //make sure all the infor in the buffer could be read
        if err := cmd_grep.Wait(); err != nil {
                fmt.Println("Error happened in Wait process")
                return
        }
        fmt.Println(buf_result.String())

}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM