The exec
package in Go is designed to run external commands. It is similar to the os/exec
package, but provides a simpler interface and does not support streaming.
- The main difference between
exec
andos/exec
is that the process does not wait for child processes to finish. This is useful for running long-running processes that are expected to run in the background.
out, err := exec.Command("date").Output()
if err != nil {
log.Fatal(err)
}
fmt.Printf("The date is %s\n", out)
This example runs the date
command and logs any errors that occur.