MCPcopy Create free account
hub / github.com/mmcgrana/gobyexample / main

Function main

examples/execing-processes/execing-processes.go:19–50  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

17)
18
19func main() {
20
21 // For our example we'll exec `ls`. Go requires an
22 // absolute path to the binary we want to execute, so
23 // we'll use `exec.LookPath` to find it (probably
24 // `/bin/ls`).
25 binary, lookErr := exec.LookPath("ls")
26 if lookErr != nil {
27 panic(lookErr)
28 }
29
30 // `Exec` requires arguments in slice form (as
31 // opposed to one big string). We'll give `ls` a few
32 // common arguments. Note that the first argument should
33 // be the program name.
34 args := []string{"ls", "-a", "-l", "-h"}
35
36 // `Exec` also needs a set of [environment variables](environment-variables)
37 // to use. Here we just provide our current
38 // environment.
39 env := os.Environ()
40
41 // Here's the actual `syscall.Exec` call. If this call is
42 // successful, the execution of our process will end
43 // here and be replaced by the `/bin/ls -a -l -h`
44 // process. If there is an error we'll get a return
45 // value.
46 execErr := syscall.Exec(binary, args, env)
47 if execErr != nil {
48 panic(execErr)
49 }
50}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected