golang 学习笔记 使用cmd
阅读原文时间:2023年07月16日阅读:2

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())

}