Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
dabf3db7
Commit
dabf3db7
authored
Feb 15, 2012
by
Brad Fitzpatrick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
os/exec: add some examples
R=golang-dev, adg, r, bradfitz CC=golang-dev
https://golang.org/cl/5675054
parent
eccc22e5
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
75 additions
and
0 deletions
+75
-0
src/pkg/os/exec/example_test.go
src/pkg/os/exec/example_test.go
+75
-0
No files found.
src/pkg/os/exec/example_test.go
0 → 100644
View file @
dabf3db7
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package
exec_test
import
(
"bytes"
"encoding/json"
"fmt"
"log"
"os/exec"
"strings"
)
func
ExampleLookPath
()
{
path
,
err
:=
exec
.
LookPath
(
"fortune"
)
if
err
!=
nil
{
log
.
Fatal
(
"installing fortune is in your future"
)
}
fmt
.
Printf
(
"fortune is available at %s
\n
"
,
path
)
}
func
ExampleCommand
()
{
cmd
:=
exec
.
Command
(
"tr"
,
"a-z"
,
"A-Z"
)
cmd
.
Stdin
=
strings
.
NewReader
(
"some input"
)
var
out
bytes
.
Buffer
cmd
.
Stdout
=
&
out
err
:=
cmd
.
Run
()
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
fmt
.
Printf
(
"in all caps: %q
\n
"
,
out
.
String
())
}
func
ExampleCmd_Output
()
{
out
,
err
:=
exec
.
Command
(
"date"
)
.
Output
()
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
fmt
.
Printf
(
"The date is %s
\n
"
,
out
)
}
func
ExampleCmd_Start
()
{
cmd
:=
exec
.
Command
(
"sleep"
,
"5"
)
err
:=
cmd
.
Start
()
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
log
.
Printf
(
"Waiting for command to finish..."
)
err
=
cmd
.
Wait
()
log
.
Printf
(
"Command finished with error: %v"
,
err
)
}
func
ExampleCmd_StdoutPipe
()
{
cmd
:=
exec
.
Command
(
"echo"
,
"-n"
,
`{"Name": "Bob", "Age": 32}`
)
stdout
,
err
:=
cmd
.
StdoutPipe
()
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
if
err
:=
cmd
.
Start
();
err
!=
nil
{
log
.
Fatal
(
err
)
}
var
person
struct
{
Name
string
Age
int
}
if
err
:=
json
.
NewDecoder
(
stdout
)
.
Decode
(
&
person
);
err
!=
nil
{
log
.
Fatal
(
err
)
}
if
err
:=
cmd
.
Wait
();
err
!=
nil
{
log
.
Fatal
(
err
)
}
fmt
.
Printf
(
"%s is %d years old
\n
"
,
person
.
Name
,
person
.
Age
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment