Skip to content

Commit

Permalink
os/exec: add example for CommandContext
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
alexd765 authored and bradfitz committed Oct 5, 2016
1 parent b9fd510 commit a9b4953
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/os/exec/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ package exec_test

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"os/exec"
"strings"
"time"
)

func ExampleLookPath() {
Expand Down Expand Up @@ -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.
}
}

0 comments on commit a9b4953

Please sign in to comment.