diff options
| author | Alexander Döring <email@alexd.ch> | 2016-10-02 21:07:40 +0200 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2016-10-05 21:30:14 +0000 |
| commit | a9b49537771c05b82923a256a47b73af98c3e87e (patch) | |
| tree | 3ce7d16495cbbe9e0763d8abf60a1ab11dd13e35 /src/os/exec/example_test.go | |
| parent | b9fd510cd00b6aa26e2ea7001a07b90ebf97d2ed (diff) | |
| download | go-a9b49537771c05b82923a256a47b73af98c3e87e.tar.xz | |
os/exec: add example for CommandContext
Updates #16360
Change-Id: I0e0afe7a89f2ebcb3e5bbc345f77a605d3afc398
Reviewed-on: https://go-review.googlesource.com/30103
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/os/exec/example_test.go')
| -rw-r--r-- | src/os/exec/example_test.go | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/src/os/exec/example_test.go b/src/os/exec/example_test.go index a38e2889e6..5ccb21af6a 100644 --- a/src/os/exec/example_test.go +++ b/src/os/exec/example_test.go @@ -6,6 +6,7 @@ package exec_test import ( "bytes" + "context" "encoding/json" "fmt" "io" @@ -13,6 +14,7 @@ import ( "log" "os/exec" "strings" + "time" ) func ExampleLookPath() { @@ -123,3 +125,13 @@ func ExampleCmd_CombinedOutput() { } fmt.Printf("%s\n", stdoutStderr) } + +func ExampleCommandContext() { + ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) + defer cancel() + + if err := exec.CommandContext(ctx, "sleep", "5").Run(); err != nil { + // This will fail after 100 milliseconds. The 5 second sleep + // will be interrupted. + } +} |
