How do I spawn and manage subprocesses in Go?

In Go, you can spawn and manage subprocesses using the os/exec package, which provides a way to run external commands and obtain their output. Below is an example illustrating how to do this.

package main import ( "fmt" "os/exec" ) func main() { // Create a command to run "ls" which lists directory contents cmd := exec.Command("ls", "-l") // Run the command output, err := cmd.CombinedOutput() if err != nil { fmt.Println("Error:", err) return } // Print the output fmt.Println(string(output)) }

Go subprocess os/exec command execution process management