Spawning Process _ Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Sometimes our Go programs need to spawn other, non-go process. For example, the syntax highlighting on this site was implemented by spawning a pygmentize process from a Go program. Let's look at a few examples of spawning processes from Go

Package Mainimport ("FMT"    "Io/ioutil"    "os/exec") Func main () {datecmd:= Exec.command ("Date") Dateout, err:=datecmd.output ()ifErr! =Nil {panic (err)} FMT. Println ("> Date") fmt. Println (string(dateout)) Grepcmd:= Exec.command ("grep","Hello") Grepin, _:=grepcmd.stdinpipe () Grepout, _:=grepcmd.stdoutpipe () Grepcmd.start () Grepin.write ([]byte("Hello Grep\ngoodbye grep") ) Grepin.close () Grepbytes, _:=Ioutil. ReadAll (Grepout) grepcmd.wait () fmt. Println ("> grep Hello") fmt. Println (string(grepbytes)) Lscmd:= Exec.command ("Bash","- C","ls-a-l-h") Lsout, err:=lscmd.output ()ifErr! =Nil {panic (err)} FMT. Println ("> Ls-a-l-h") fmt. Println (string(lsout))}
>datewed APR1  -: the: +Cst ->grep hellohello grep> Ls-a-L-Htotal3648drwxr-xr-x -XJK Staff 544B APR1  -: the. DRWXR-xr-x+ -XJK Staff1.0K Mar6  -:xx ..-rw-r--r--@1XJK Staff6.0K Mar -  One: + . Ds_store-rw-r--r--1XJK Staff 838B Mar -  -: -Reading.go-rw-r--r--1XJK Staff 553B Mar -  -: AUrl.go-rw-r--r--1XJK Staff 598B Mar -  -: $Writing.go-rw-r--r--1XJK Staff 402B Mar -  -: atBase64.go-rw-r--r--1XJK Staff 447B Mar to  -: -Commandlineflags.go-rwxr-xr-x1XJK Staff1.7MMar to  -: +commonlineargument-rw-r--r--1XJK Staff 209B Mar to  -: -Commonlineargument.go-rw-r--r--1XJK Staff 261B APR1  -: GenevaEnvironmentvariables.go-rw-r--r--1XJK Staff 298B Mar to  -: theLinefilters.go-rw-r--r--1XJK Staff 384B Mar -  -:GenevaNumber_parsing.go-rw-r--r--1XJK Staff 189B Mar -  -: -Sha.go-rw-r--r--1XJK Staff 720B APR1  -: theSpawningprocess.go-rwxr-xr-x1XJK Staff 243B Mar -  One: -Urllib001.p

Summarize:

1: ...

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.