In the previous example we looked at
spawning external processes. We
do this when we need an external process accessible to
a running Go process. Sometimes we just want to
completely replace the current Go process with another
(perhaps non-Go) one. To do this we’ll use Go’s
implementation of the classic
|
|
![]() ![]() package main |
|
import ( "os" "os/exec" "syscall" ) |
|
func main() { |
|
For our example we’ll exec |
binary, lookErr := exec.LookPath("ls") if lookErr != nil { panic(lookErr) } |
|
args := []string{"ls", "-a", "-l", "-h"} |
|
env := os.Environ() |
Here’s the actual |
execErr := syscall.Exec(binary, args, env) if execErr != nil { panic(execErr) } } |
When we run our program it is replaced by |
$ go run execing-processes.go total 16 drwxr-xr-x 4 mark 136B Oct 3 16:29 . drwxr-xr-x 91 mark 3.0K Oct 3 12:50 .. -rw-r--r-- 1 mark 1.3K Oct 3 16:28 execing-processes.go |
Note that Go does not offer a classic Unix |
Next example: Signals.