Golang interpretation (Go language) standard library analysis Os/exec

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

Golang Standard Library


Today we talk about os/exec bag, this I will not nonsense

(1) func LookPath(file string) (f string, err error) This is the path to the search for the executable binary file, which returns the execution path and error
[PHP]
Import (
"FMT"
"Os/exec"
)

Func Main () {
F, err: = Exec. Lookpath ("PHP")
If err! = Nil {
Fmt. PRINTLN ("Not install PHP")
}
Fmt. Println (f)//output of my Phpd:\php\\php.exe
}
[/php]

(2) Type CMD
[PHP]
Type CMD struct {
Path string
Args []string
ENV []string
Dir string
Stdin io. Reader
Stdout io. Writer
Stderr io. Writer
Extrafiles []*os. File
Sysprocattr *syscall. Sysprocattr
Process *os. Process
Processstate *os. Processstate
}
[/php]

[1] func Command(name string, arg ...string) *Cmd The path to the input file, the parameter string, and the structure above the structure of the *cmd is returned.
[PHP]
Import (
"FMT"
"Os/exec"
)

Func Main () {
argv: = []string{"-A"}
c: = Exec.command ("ls", argv ...)
D, _: = C.output ()
Fmt. Println (String (d))//Because GIT bash is loaded so you can use Ls-a
/*
*.
*..
*command.go
*lookpath.go
*/
}
[/php]

[2] func (c *Cmd) CombinedOutput() ([]byte, error) This function returns the standard output and error
[PHP]
Import (
"FMT"
"Os/exec"
)

Func Main () {
ARV: = []string{"-A"}
c: = Exec.command ("ls", arv ...)
D, Err: = C.combinedoutput ()
If err! = Nil {
Fmt. PRINTLN (ERR)
}
Fmt. Println (String (d))//output as in command, ha
}
[/php]

[3] func (c *Cmd) Output() ([]byte, error) We used it for the first time, back to the standard output.
[PHP]
Import (
"FMT"
"Os/exec"
)

Func Main () {
C, Err: = Exec.command ("date"). Output ()
If err! = Nil {
Fmt. PRINTLN (ERR)
}
Fmt. Println (String (c))//sat Jan 4 17:07:36 2014 This is an example from the standard library.
}
[/php]

[4] func (c *Cmd) Run() error This function primarily executes the command in *cmd, and waits for the command to complete, returning an error message if the command execution is unsuccessful
[PHP]
Import (
"FMT"
"Os/exec"
)

Func Main () {
C, Err: = Exec.command ("date")
If err! = Nil {
Fmt. PRINTLN (ERR)
}
Err = C.run ()
If err! = Nil {
Fmt. PRINTLN (ERR)
}
D, _: = C.output ()
Fmt. Println (String (d))//sat Jan 4 17:07:36
}
[/php]

[5] func (c *Cmd) Start() error This function basically executes the command in *cmd, just lets the command start executing, and does not wait for the command to finish executing.

[6] func (c *Cmd) Wait() error This function is waiting for the function to complete
[PHP]
Import (
"FMT"
"Os/exec"
)

Func Main () {
An example of an official standard library
CMD: = Exec.command ("Sleep", "5")//Execution wait 5 seconds
ERR: = cmd. Start ()//Begin execution
If err! = Nil {
Fmt. Printf ("Error:%s\n", err)
Return
}
Fmt. Printf ("Waiting for Command to finish...\n")
Err = cmd. Wait ()//wait the function below waits for execution to complete
Fmt. Printf ("Command finished with error:%v\n", err)
}
[/php]

[7] func (c *Cmd) StderrPipe() (io.ReadCloser, error) This function is primarily used to connect to a pipeline where the command starts with an error standard output, and the pipe shuts down automatically when the command ends
[PHP]
Import (
"FMT"
"Io/ioutil"
"Os/exec"
)

Func Main () {
c: = Exec.command ("mv", "Hello")
I, Err: = C.stderrpipe ()
If err! = Nil {
Fmt. Printf ("Error:%s\n", err)
Return
}
If Err = C.start (); Err! = Nil {
Fmt. Printf ("Error:%s\n", err)
}
B, _: = Ioutil. ReadAll (i)
If err: = C.wait (); Err! = Nil {
Fmt. Printf ("Error:%s\n", err)//error:exit status 1 mv:missing file argument Try ' Mv–help ' for more information.
}
Fmt. Println (string (b))
}
[/php]

[8] func (c *Cmd) StdinPipe() (io.WriteCloser, error) This function is primarily used to connect to a standard input pipeline when the command is started
[PHP]
Import (
"Bytes"
"FMT"
"Os/exec"
)

Func Main () {
var output bytes. Buffer
CMD: = Exec.command ("Cat")
Cmd. Stdout = &output
stdin, _: = cmd. Stdinpipe ()
Cmd. Start ()//execution begin
Stdin. Write ([]byte ("Widuu test"))
Stdin. Close ()
Cmd. Wait ()//waiting for execution to complete
Fmt. Printf ("The output is:%s\n", output.) Bytes ())//the output Is:widuu test!
}
[/php]

[9] With input, there is outputfunc (c *Cmd) StdoutPipe() (io.ReadCloser, error)
[PHP]
Import (
"FMT"
"Io/ioutil"
"Os/exec"
)

Func Main () {
cmd: = Exec.command ("ls", "-ll")
stdout, _: = cmd. Stdoutpipe ()
cmd. Start ()
D, _: = Ioutil. ReadAll (STDOUT)//Read Data
cmd from the pipe. Wait ()
FMT. Println (String (d))
/*
Total 5
-rw-r–r–1 administ administ 268 Jan 4 17:07 combinedoutput.go
-rw-r–r–1 Admin Ist administ 277 Jan 4 17:01 command.go
-rw-r–r–1 administ administ 209 Jan 4 16:46 lookpath.go
-rw-r–r–1 Administ Administ 241 Jan 4 17:07 output.go
-rw-r–r–1 administ administ 271 Jan 4 17:13 run.go
-rw-r–r–1 administ Administ 438 Jan 4 17:24 start.go
-rw-r–r–1 administ administ 497 Jan 4 17:32 stderrpipe.go
-rw-r–r–1 administ Administ 55 2 Jan 6 14:10 stdinpipe.go
-rw-r–r–1 administ administ 235 Jan 6 14:14 stdoutpipe.go
*/
}
[/php]

(3) The type error structure is
[PHP]
Type Error struct {
Name string
ERR Error
}
[/php]
[1] func (e *Error) Error() string This function is mainly the error message that the output command failed to execute.
[PHP]
Import (
"Errors"
"FMT"
"Os/exec"
)

Func Main () {
ERR: = &exec. error{
Name: "Widuu",
Err:errors. New ("is NOT exists Widuu"),
}

Fmt. Println (Err. Error ())//exec: "Widuu": Is NOT exists widuu
}
[/php]

(4)type ExitError
[PHP]
Type Exiterror struct {
*os. Processstate
}
[/php]
[1] func (e *ExitError) Error() string This function basically returns a message that executes an unsuccessful command.
[PHP]
Import (
"FMT"
"Os/exec"
)

Func Main () {
CMD: = Exec.command ("mv", "Hello world!")
Cmd. Run ()
Exiterror: = Exec. Exiterror{cmd. Processstate}
Fmt. Printf ("The output is:%s\n", Exiterror.error ())//the output is:exit status 1
}
[/php]
Here we have the Exec pack on the finish, this article starts in my blog, if you like can always follow my blog

without permission, may not reprint this station any article: the Micro Degree Network»golang Explanation (go language) standard library analysis Os/exec

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.