lean into examples

This commit is contained in:
Mark McGranaghan
2012-10-09 21:02:12 -07:00
parent 5d1775bdaa
commit 8daa226a48
130 changed files with 4 additions and 4 deletions

View File

@@ -0,0 +1,41 @@
// In the previous example we looked at spawning external
// process. We do this when we need the functionality
// of another process accessable to a running Go process.
// In other cases we may just want to completely replace
// the current Go process with another process. To do
// this we'll use Go's implementation of the `exec`.
// In this example we'll exec an `ls` command.
package main
import "syscall"
import "os"
import "os/exec"
func main() {
// We'll need an absolute path to the binary we'd
// like to execute. In this case we'll get the path
// for `ls`, 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). Here we'll give `ls`
// a few arguments
args := []string{"-a", "-l", "-h"}
// We'll give the command we execute our current
// environment.
env := os.Environ()
// The actual exec call. If this call is succesful,
// the execution of our process will end here and it
// will 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 {
panic(execErr)
}
}

View File

@@ -0,0 +1,14 @@
# Now if we run this we'll see our programm replaced
# by `ls`.
$ go run execing-processes.go
$ ls -a -l -h
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 `fork`
# function. Usually this isn't an issue though, since
# starting goroutines, spawning processes, and execing
# processes covers most use cases for `fork`.