Skip to content

Commit a9b4953

Browse files
alexd765bradfitz
authored andcommitted
os/exec: add example for CommandContext
Updates #16360 Change-Id: I0e0afe7a89f2ebcb3e5bbc345f77a605d3afc398 Reviewed-on: https://go-review.googlesource.com/30103 Reviewed-by: Brad Fitzpatrick <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent b9fd510 commit a9b4953

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/os/exec/example_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ package exec_test
66

77
import (
88
"bytes"
9+
"context"
910
"encoding/json"
1011
"fmt"
1112
"io"
1213
"io/ioutil"
1314
"log"
1415
"os/exec"
1516
"strings"
17+
"time"
1618
)
1719

1820
func ExampleLookPath() {
@@ -123,3 +125,13 @@ func ExampleCmd_CombinedOutput() {
123125
}
124126
fmt.Printf("%s\n", stdoutStderr)
125127
}
128+
129+
func ExampleCommandContext() {
130+
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
131+
defer cancel()
132+
133+
if err := exec.CommandContext(ctx, "sleep", "5").Run(); err != nil {
134+
// This will fail after 100 milliseconds. The 5 second sleep
135+
// will be interrupted.
136+
}
137+
}

0 commit comments

Comments
 (0)