func Command(name string, arg ...string) *Cmd
1.1、此函数的作用

创建一个指向Cmd结构的指针。

1.2、返回值说明

Cmd的定义在os/exec/exec.go中,如下:

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

	ctx             context.Context // nil means none
	lookPathErr     error           // LookPath error, if any.
	finished        bool            // when Wait was called
	childFiles      []*os.File
	closeAfterStart []io.Closer
	closeAfterWait  []io.Closer
	goroutine       []func() error
	errch           chan error // one send per goroutine
	waitDone        chan struct{}
}
1.3、使用示例
package main

import (
    "os"
    "os/exec"
)

func main() {
    cmd := exec.Command("ls", "-lah")

    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr

    err := cmd.Run()

    if err == nil {
        os.Exit(-1)
    } else {
        os.Exit(0)
    }
}

使用go命令编译并运行:

go run test.go