Study and use of os/exec package in go language

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.