File tree Expand file tree Collapse file tree 3 files changed +42
-0
lines changed
Expand file tree Collapse file tree 3 files changed +42
-0
lines changed Original file line number Diff line number Diff line change 1212| multi\_ command\_ with\_ pipe.go| cmdexec\_ multi\_ command\_ with\_ pipe| 複数の (* exec.Cmd) をパイプストリームで繋いで実行するサンプルです|
1313| withenv.go| cmdexec\_ env| * exec.Cmd 実行時に追加の環境変数を指定するサンプルです|
1414| withdir.go| cmdexec\_ dir| * exec.Cmd 実行時にワーキングディレクトリを指定するサンプルです|
15+ | withslice.go| cmdexec\_ slice| * exec.Cmd 実行時にスライスの値をコマンドの引数で指定するサンプルです|
Original file line number Diff line number Diff line change @@ -21,4 +21,5 @@ func (r *register) Regist(m mapping.ExampleMapping) {
2121 m ["cmdexec_multi_command_with_pipe" ] = MultiCommandWithPipe
2222 m ["cmdexec_env" ] = WithEnv
2323 m ["cmdexec_dir" ] = WithDir
24+ m ["cmdexec_slice" ] = WithSlice
2425}
Original file line number Diff line number Diff line change 1+ package cmdexec
2+
3+ import (
4+ "os/exec"
5+
6+ "github.com/devlights/gomy/output"
7+ )
8+
9+ // WithSlice -- *exec.Cmd 実行時にスライスの値をコマンドの引数で指定するサンプルです.
10+ //
11+ // REFERENCES
12+ // - https://dev.to/tobychui/quick-notes-for-go-os-exec-3ejg
13+ func WithSlice () error {
14+ var (
15+ cmd * exec.Cmd
16+ out []byte
17+ err error
18+ )
19+
20+ var (
21+ p = []string {
22+ "hello" ,
23+ "world" ,
24+ "こんにちわ" ,
25+ "世界" ,
26+ }
27+ )
28+
29+ cmd = exec .Command ("echo" , p ... )
30+
31+ out , err = cmd .CombinedOutput ()
32+ if err != nil {
33+ output .Stdoutf ("[cmd error]" , "%s" , out )
34+ return err
35+ }
36+
37+ output .Stdoutf ("[cmd]" , "%s" , out )
38+
39+ return nil
40+ }
You can’t perform that action at this time.
0 commit comments