我正在尝试将参数传递给 exec.Command。该论点的一部分是变量。a := fileNameexec.Command("command", "/path/to/"a).Output()我不确定如何解决这个问题,我想我需要在通过它之前完全形成论点,但我也在努力解决这个问题。我不知道该怎么做:a := fileNamearg := "/path/to/"aexec.Command("command", arg).Output()
2 回答

HUX布斯
TA贡献1876条经验 获得超6个赞
在 Go 中,字符串与+
,
exec.Command("command", "/path/to/" + a)
您还可以使用格式化功能
exec.Command("command", fmt.Sprintf("/path/to/%s", a))
但在这种情况下,它可能更适合使用filepath.Join
dir := "/path/to/"exec.Command ("command", filepath.Join(dir, a))

呼啦一阵风
TA贡献1802条经验 获得超6个赞
我通常使用这种方法:
a := fileName
cmdArgs := []string{"/path/to/" + a, "morearg"}
out, err := exec.Command("command", cmdArgs...).Output()
- 2 回答
- 0 关注
- 335 浏览
添加回答
举报
0/150
提交
取消