Start processes with exec.Command, access the PID via cmd.Process.Pid, and manage them using os.Process methods like Wait and Signal.
Use the os/exec package to start processes and the os package to manage them by PID. Create a process with exec.Command, start it to get a *os.Process containing the PID, and use Wait() to block until completion or Signal() to send signals.
package main
import (
"os/exec"
"fmt"
"os"
)
func main() {
cmd := exec.Command("sleep", "10")
if err := cmd.Start(); err != nil {
panic(err)
}
fmt.Println("PID:", cmd.Process.Pid)
cmd.Process.Signal(os.Interrupt)
cmd.Wait()
}
Processes are independent programs running on your computer, each identified by a unique number called a PID. In Go, you use the os/exec package to launch these programs and the os package to control them, like stopping or waiting for them to finish. Think of it like a manager hiring a worker (starting the process), giving them an ID badge (the PID), and then either waiting for their report or telling them to stop.