Commit a9b49537 authored by Alexander Döring's avatar Alexander Döring Committed by Brad Fitzpatrick

os/exec: add example for CommandContext

Updates #16360

Change-Id: I0e0afe7a89f2ebcb3e5bbc345f77a605d3afc398
Reviewed-on: https://go-review.googlesource.com/30103Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent b9fd510c
...@@ -6,6 +6,7 @@ package exec_test ...@@ -6,6 +6,7 @@ package exec_test
import ( import (
"bytes" "bytes"
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
...@@ -13,6 +14,7 @@ import ( ...@@ -13,6 +14,7 @@ import (
"log" "log"
"os/exec" "os/exec"
"strings" "strings"
"time"
) )
func ExampleLookPath() { func ExampleLookPath() {
...@@ -123,3 +125,13 @@ func ExampleCmd_CombinedOutput() { ...@@ -123,3 +125,13 @@ func ExampleCmd_CombinedOutput() {
} }
fmt.Printf("%s\n", stdoutStderr) 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.
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment