Package Main;import ("Os/exec" "FMT" "Io/ioutil" "bytes") Func main () {//Find executable binaries in environment variable path/// Returns the full path or a relative path relative to the current directory file, _: = Exec. Lookpath ("Go"); fmt. Println (file);//Returns a cmdcmd: = Exec.command ("Go", "version"),//executes the command and returns the standard output and error output out, _: = Cmd.combinedoutput (), FMT. Println (string out);//Create a CMDCMD2: = Exec.command ("ping", "www.baidu.com"); buf: = bytes. The buffer{};//sets the standard output of the CMD2 to BUFCMD2. Stdout = &buf;//runs the command, blocking until the CMD2 is complete. Run (); fmt. Println (BUF. String ());//Create a cmdcmd3: = Exec.command ("ping", "www.baidu.com");//get command after start standard output pipe out3, _: = Cmd3. Stdoutpipe ();//execute command cmd3. Start ();//Read all data in the pipeline Data3, _: = Ioutil. ReadAll (OUT3);//wait for command execution to complete cmd3. Wait (); fmt. Println (String (DATA3));}
Study and use of os/exec package in go language