Skip to content

Commit 566e414

Browse files
committed
Add cmdexec/withslice.go
1 parent 69979a8 commit 566e414

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

examples/basic/cmdexec/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
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 実行時にスライスの値をコマンドの引数で指定するサンプルです|

examples/basic/cmdexec/examples.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
}

0 commit comments

Comments
 (0)