Golang Exec 调用外部程序 | KaiQ.Gu|KerwinKoo Blog

JerryXia 发表于 , 阅读 (0)

用法e.g.

import ""os/exec""
1
2
3
4
5
6
7
func FetchDefaultTemplate() {
cmd := exec.Command("git", "clone", "--depth=1", "https://github.com/mengzhuo/bla_default_template.git")
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
cmd.Wait()
}

cmd.start()官方说明:

1
2
3
4
5
6
// Start starts the specified command but does not wait for it to complete.
//
// The Wait method will return the exit code and release associated resources
// once the command exits.

func (c *Cmd) Start() error

关于cmd.Wait()的官方说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Wait waits for the command to exit.
// It 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 c.Stdin is not an *os.File, Wait also waits for the I/O loop
// copying from c.Stdin into the process's standard input
// to complete.
//
// Wait releases any resources associated with the Cmd.

func (c *Cmd) Wait() error