index
In the previous example we looked at [spawning external processes](spawning-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 <a href="http://en.wikipedia.org/wiki/Exec_(operating_system)"><code>exec</code></a> function.
package main
import (
	"os"
	"os/exec"
	"syscall"
)
func main() {
For our example we'll exec `ls`. Go requires an absolute path to the binary we want to execute, so we'll use `exec.LookPath` to find it (probably `/bin/ls`).
	binary, lookErr := exec.LookPath("ls")
	if lookErr != nil {
		panic(lookErr)
	}
`Exec` requires arguments in slice form (as apposed to one big string). We'll give `ls` a few common arguments. Note that the first argument should be the program name.
	args := []string{"ls", "-a", "-l", "-h"}
`Exec` also needs a set of [environment variables](environment-variables) to use. Here we just provide our current environment.
	env := os.Environ()
Here's the actual `syscall.Exec` call. If this call is successful, the execution of our process will end here and be replaced by the `/bin/ls -a -l -h` process. If there is an error we'll get a return value.
	execErr := syscall.Exec(binary, args, env)
	if execErr != nil {
total 4.1M
drwxrwxr-x 8 travis travis 4.0K Oct 21 16:50 .
drwxrwxr-x 3 travis travis 4.0K Oct 21 16:45 ..
drwxrwxr-x 2 travis travis 4.0K Oct 21 16:52 examples
drwxrwxr-x 8 travis travis 4.0K Oct 21 16:45 .git
-rw-rw-r-- 1 travis travis   33 Oct 21 16:45 .gitignore
-rw-rw-r-- 1 travis travis 1.3K Oct 21 16:45 Makefile
-rwxrwxr-x 1 travis travis 4.1M Oct 21 16:46 maketable
-rw-rw-r-- 1 travis travis 1.8K Oct 21 16:45 README.md
drwxrwxr-x 2 travis travis 4.0K Oct 21 16:45 site
drwxr-xr-x 2 travis travis 4.0K Oct 21 16:50 subdir
drwxrwxr-x 3 travis travis 4.0K Oct 21 16:46 tmp
drwxrwxr-x 2 travis travis 4.0K Oct 21 16:45 tools
-rw-rw-r-- 1 travis travis  106 Oct 21 16:45 .travis.yml
		panic(execErr)
	}
}
index