File tree 2 files changed +17
-0
lines changed
2 files changed +17
-0
lines changed Original file line number Diff line number Diff line change 1
1
package execingprocesses
2
2
3
+ // Similarly to spawning a subprocess
3
4
func Run () {
4
5
5
6
}
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package spawningprocesses
2
2
3
3
import (
4
4
"fmt"
5
+ "io"
5
6
"os/exec"
6
7
)
7
8
@@ -14,6 +15,21 @@ func Run() {
14
15
commandWithArgs ("date" , "-u" )
15
16
// error with args:
16
17
commandWithArgs ("date" , "-x" )
18
+ // piping basics
19
+ commandWithPiping ("grep" , "hello" )
20
+ }
21
+
22
+ func commandWithPiping (cmd string , args ... string ) {
23
+ grepCommand := exec .Command (cmd , args ... )
24
+ in , _ := grepCommand .StdinPipe ()
25
+ out , _ := grepCommand .StdoutPipe ()
26
+ grepCommand .Start ()
27
+ in .Write ([]byte ("hello grep\n goodbye grep" ))
28
+ in .Close ()
29
+ grepBytes , _ := io .ReadAll (out )
30
+ grepCommand .Wait ()
31
+ fmt .Println ("> grep hello" )
32
+ fmt .Println (string (grepBytes ))
17
33
}
18
34
19
35
// A very basic command without any flags/args provided.
You can’t perform that action at this time.
0 commit comments