]> Cypherpunks repositories - gostls13.git/commitdiff
os/exec: add example for CommandContext
authorAlexander Döring <email@alexd.ch>
Sun, 2 Oct 2016 19:07:40 +0000 (21:07 +0200)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 5 Oct 2016 21:30:14 +0000 (21:30 +0000)
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>

src/os/exec/example_test.go

index a38e2889e6470b00c903dee8cac946ac9f451528..5ccb21af6adc11bd09ecdfe55a3cceb867aac197 100644 (file)
@@ -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.
+       }
+}