Go Package Learning--os/exec

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

Package exec is used to run external commands, which encapsulate the OS. StartProcess, which makes the operation easier.

Index

    • Variables
    • Func lookpath (File string) (String, error)
    • Type CMD
      • Func Command (name string, arg ... string) *cmd
      • Func (c *cmd) Combinedoutput () ([]byte, error)
      • Func (c *cmd) Output () ([]byte, error)
      • Func (c *cmd) Run () error
      • Func (c *cmd) Start () error
      • Func (c *cmd) stderrpipe () (IO. Readcloser, error)
      • Func (c *cmd) stdinpipe () (IO. Writecloser, error)
      • Func (c *cmd) stdoutpipe () (IO. Readcloser, error)
      • Func (c *cmd) Wait () error
    • Type Error
      • Func (e *error) Error () string
    • Type Exiterror
      • Func (e *exiterror) Error () string

Examples

    • Cmd.output
    • Cmd.start
    • Cmd.stdoutpipe
    • Command
    • Lookpath

Package Files

Exec.go Lp_unix.go

1. Basic functions

Func Lookpath

Func lookpath (File string) (String, error)
Lookpath finds the path information for this binary in the PATH environment variable based on the given binary file name file:

Path, err: = Exec. Lookpath ("Fortune") if err! = Nil {    log. Fatal ("Installing Fortune is in your")}fmt. Printf ("Fortune is available at%s\n", path)
2. Type CMD

The type Cmd struct {        Path string    Args []string    Dir string    Stdin io. Reader    Stdout io. Writer    Stderr io. Writer    extrafiles []*os. File    sysprocattr *syscall. Sysprocattr    Process *os. Process    processstate *os. Processstate}
CMD represents an external command, which is either ready or already in run.

Func Command

Func Command (name string, arg ... string) *cmd
command returns the CMD structure used to execute the commands of name, and path and args are set in cmd, and the other fields are empty. Examples are as follows:

CMD: = Exec.command ("tr", "A-Z", "A-Z") cmd. Stdin = strings. Newreader ("some input") var out bytes. Buffercmd.stdout = &outerr: = cmd. Run () if err! = Nil {    log. Fatal (Err)}fmt. Printf ("In all caps:%q\n"), out. String ())

Func (*cmd) combinedoutput

Func (c *cmd) Combinedoutput () ([]byte, error)
Run command C, and return combined output and error.

Func (*cmd) Output

Func (c *cmd) Output () ([]byte, error)
Run command C and return to standard output, as in the following example:

Out, err: = Exec.command ("date"). Output () if err! = Nil {    log. Fatal (Err)}fmt. Printf ("The date is%s\n", out)

Func (*cmd) Run

Func (c *cmd) Run () error
Run runs the specified command C and waits for it to complete.

Func (*cmd) Start

Func (c *cmd) Start () error
Start also runs a specified command C, but does not wait for it to complete. Examples are as follows:

CMD: = Exec.command ("Sleep", "5") Err: = cmd. Start () if err! = Nil {    log. Fatal (Err)}log. Printf ("Waiting for command to finish ...") Err = cmd. Wait () log. Printf ("Command finished with error:%v", err)

Func (*cmd) Wait

Func (c *cmd) Wait () error
Wait waits for command C to exit, and command C must be run by the Start method. Will it clog up there?

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.