os/exec

  • func Command(name string, arg ...string) *Cmd
  • func (c *Cmd) Run() error

func Command(name string, arg ...string) *Cmd

package main

import (
    "bytes"
    "fmt"
    "log"
    "os/exec"
    "strings"
)

func main() {
    cmd := exec.Command("tr", "a-z", "A-Z")
    cmd.Stdin = strings.NewReader("some input")
    var out bytes.Buffer
    cmd.Stdout = &out
    err := cmd.Run()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("in all caps: %q\n", out.String()) //2009/11/10 23:00:00 exec: "tr": executable file not found in $PATH
}

func (c *Cmd) Run() error

Run starts the specified command and waits for it to complete.

The returned error is nil if the command runs, has no problems copying stdin, stdout, and stderr, and exits with a zero exit status. If the command starts but does not complete successfully, the error is of type *ExitError. Other error types may be returned for other situations.

If the calling goroutine has locked the operating system thread with runtime.LockOSThread and modified any inheritable OS-level thread state (for example, Linux or Plan 9 name spaces), the new process will inherit the caller's thread state.

package main

import (
    "log"
    "os/exec"
)

func main() {
    cmd := exec.Command("sleep", "1")
    log.Printf("Running command and waiting for it to finish...")
    err := cmd.Run()
    log.Printf("Command finished with error: %v", err)
}

results matching ""

    No results matching ""