type Cmd(struct)
Cmd represents an external command being prepared or run.
A Cmd cannot be reused after calling its Run, Output or CombinedOutput
methods.
Args holds command line arguments, including the command as Args[0].
If the Args field is empty or nil, Run uses {Path}.
In typical use, both Path and Args are set by calling Command.
Dir specifies the working directory of the command.
If Dir is the empty string, Run runs the command in the
calling process's current directory.
Env specifies the environment of the process.
Each entry is of the form "key=value".
If Env is nil, the new process uses the current process's
environment.
If Env contains duplicate environment keys, only the last
value in the slice for each duplicate key is used.
As a special case on Windows, SYSTEMROOT is always added if
missing and not explicitly set to the empty string.
ExtraFiles specifies additional open files to be inherited by the
new process. It does not include standard input, standard output, or
standard error. If non-nil, entry i becomes file descriptor 3+i.
ExtraFiles is not supported on Windows.
Path is the path of the command to run.
This is the only field that must be set to a non-zero
value. If Path is relative, it is evaluated relative
to Dir.
Process is the underlying process, once started.
ProcessState contains information about an exited process,
available after a call to Wait or Run.
Stderrio.Writer
Stdin specifies the process's standard input.
If Stdin is nil, the process reads from the null device (os.DevNull).
If Stdin is an *os.File, the process's standard input is connected
directly to that file.
Otherwise, during the execution of the command a separate
goroutine reads from Stdin and delivers that data to the command
over a pipe. In this case, Wait does not complete until the goroutine
stops copying, either because it has reached the end of Stdin
(EOF or a read error) or because writing to the pipe returned an error.
Stdout and Stderr specify the process's standard output and error.
If either is nil, Run connects the corresponding file descriptor
to the null device (os.DevNull).
If either is an *os.File, the corresponding output from the process
is connected directly to that file.
Otherwise, during the execution of the command a separate goroutine
reads from the process over a pipe and delivers that data to the
corresponding Writer. In this case, Wait does not complete until the
goroutine reaches EOF or encounters an error.
If Stdout and Stderr are the same writer, and have a type that can
be compared with ==, at most one goroutine at a time will call Write.
SysProcAttr holds optional, operating system-specific attributes.
Run passes it to os.StartProcess as the os.ProcAttr's Sys field.
CombinedOutput runs the command and returns its combined standard
output and standard error.
Output runs the command and returns its standard output.
Any returned error will usually be of type *ExitError.
If c.Stderr was nil, Output populates ExitError.Stderr.
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.
Start starts the specified command but does not wait for it to complete.
If Start returns successfully, the c.Process field will be set.
The Wait method will return the exit code and release associated resources
once the command exits.
StderrPipe returns a pipe that will be connected to the command's
standard error when the command starts.
Wait will close the pipe after seeing the command exit, so most callers
need not close the pipe themselves. It is thus incorrect to call Wait
before all reads from the pipe have completed.
For the same reason, it is incorrect to use Run when using StderrPipe.
See the StdoutPipe example for idiomatic usage.
StdinPipe returns a pipe that will be connected to the command's
standard input when the command starts.
The pipe will be closed automatically after Wait sees the command exit.
A caller need only call Close to force the pipe to close sooner.
For example, if the command being run will not exit until standard input
is closed, the caller must close the pipe.
StdoutPipe returns a pipe that will be connected to the command's
standard output when the command starts.
Wait will close the pipe after seeing the command exit, so most callers
need not close the pipe themselves. It is thus incorrect to call Wait
before all reads from the pipe have completed.
For the same reason, it is incorrect to call Run when using StdoutPipe.
See the example for idiomatic usage.
String returns a human-readable description of c.
It is intended only for debugging.
In particular, it is not suitable for use as input to a shell.
The output of String may vary across Go releases.
Wait waits for the command to exit and waits for any copying to
stdin or copying from stdout or stderr to complete.
The command must have been started by Start.
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 fails to run or doesn't complete successfully, the
error is of type *ExitError. Other error types may be
returned for I/O problems.
If any of c.Stdin, c.Stdout or c.Stderr are not an *os.File, Wait also waits
for the respective I/O loop copying to or from the process to complete.
Wait releases any resources associated with the Cmd.
*T : expvar.Var
*T : fmt.Stringer
func Command(name string, arg ...string) *Cmd
func CommandContext(ctx context.Context, name string, arg ...string) *Cmd
func internal/testenv.CleanCmdEnv(cmd *Cmd) *Cmd
func internal/testenv.CleanCmdEnv(cmd *Cmd) *Cmd
type Error(struct)
Error is returned by LookPath when it fails to classify a file as an
executable.
Err is the underlying error.
Name is the file name for which the error occurred.
(*T) Error() string(*T) Unwrap() error
*T : error
type ExitError(struct)
An ExitError reports an unsuccessful exit by a command.
ProcessState*os.ProcessState
Stderr holds a subset of the standard error output from the
Cmd.Output method if standard error was not otherwise being
collected.
If the error output is long, Stderr may contain only a prefix
and suffix of the output, with the middle replaced with
text about the number of omitted bytes.
Stderr is provided for debugging, for inclusion in error messages.
Users with other needs should redirect Cmd.Stderr as needed.
(*T) Error() string
ExitCode returns the exit code of the exited process, or -1
if the process hasn't exited or was terminated by a signal.
Exited reports whether the program has exited.
Pid returns the process id of the exited process.
( T) String() string
Success reports whether the program exited successfully,
such as with exit status 0 on Unix.
Sys returns system-dependent exit information about
the process. Convert it to the appropriate underlying
type, such as syscall.WaitStatus on Unix, to access its contents.
SysUsage returns system-dependent resource usage information about
the exited process. Convert it to the appropriate underlying
type, such as *syscall.Rusage on Unix, to access its contents.
(On Unix, *syscall.Rusage matches struct rusage as defined in the
getrusage(2) manual page.)
SystemTime returns the system CPU time of the exited process and its children.
UserTime returns the user CPU time of the exited process and its children.
*T : error
T : expvar.Var
T : fmt.Stringer
Exported Values
func Command(name string, arg ...string) *Cmd
Command returns the Cmd struct to execute the named program with
the given arguments.
It sets only the Path and Args in the returned structure.
If name contains no path separators, Command uses LookPath to
resolve name to a complete path if possible. Otherwise it uses name
directly as Path.
The returned Cmd's Args field is constructed from the command name
followed by the elements of arg, so arg should not include the
command name itself. For example, Command("echo", "hello").
Args[0] is always name, not the possibly resolved Path.
On Windows, processes receive the whole command line as a single string
and do their own parsing. Command combines and quotes Args into a command
line string with an algorithm compatible with applications using
CommandLineToArgvW (which is the most common way). Notable exceptions are
msiexec.exe and cmd.exe (and thus, all batch files), which have a different
unquoting algorithm. In these or other similar cases, you can do the
quoting yourself and provide the full command line in SysProcAttr.CmdLine,
leaving Args empty.
func CommandContext(ctx context.Context, name string, arg ...string) *Cmd
CommandContext is like Command but includes a context.
The provided context is used to kill the process (by calling
os.Process.Kill) if the context becomes done before the command
completes on its own.
var ErrNotFounderror
ErrNotFound is the error resulting if a path search failed to find an executable file.
func LookPath(file string) (string, error)
LookPath searches for an executable named file in the
directories named by the PATH environment variable.
If file contains a slash, it is tried directly and the PATH is not consulted.
The result may be an absolute path or a path relative to the current directory.
The pages are generated with Goldsv0.1.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project and developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.