正确地将参数传递给Go Exec [英] Properly pass arguments to Go Exec

查看:140
本文介绍了正确地将参数传递给Go Exec的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力学习,首先我想尝试把超级简单的网络服务器放在一起来控制我的iTunes。我过去曾多次使用 osascript -e'告诉应用程序iTunes以playpause'多次,并认为我可以在这里简单地将呼叫拒绝给osascript。

I'm trying to learn go and as a start I wanted to try to throw together a super simple web server for controlling my iTunes. I've used osascript -e 'Tell Application "iTunes" to playpause' to this purpose many times in the past and thought I could simply sluff the call off to osascript here.

注释掉say 5命令确实有效。

The commented out "say 5" command does work.

package main

import "exec"
//import "os"

func main() {

    var command = "Tell Application 'iTunes' to playpause"
    //var command = "say 5"

    c := exec.Command("/usr/bin/osascript", "-e", command)
//  c.Stdin = os.Stdin
    _, err := c.CombinedOutput()
    println(err.String());


}

我收到的回应是如下所示 -

The response I am receiving from this is as follows -

jessed@JesseDonat-MBP ~/Desktop/goproj » ./8.out
exit status 1
[55/1536]0x1087f000

我不确定该从哪里出发和任何方向将不胜感激。

I'm not exactly sure where to go from here and any direction would be greatly appreciated.

推荐答案

我得到它与此工作

package main

import (
    "fmt"
    "exec"
)

func main() {
    command := "Tell Application \"iTunes\" to playpause"

    c := exec.Command("/usr/bin/osascript", "-e", command)
    if err := c.Run(); err != nil {
        fmt.Println(err.String())
    }
}

我认为exec.Command(...)会在参数中添加双引号,因此您只需在需要它们的地方转义\。

I think exec.Command(...) adds double quotes to the parameters if there is spaces in them, so you only need to escape \" where you need them.

这篇关于正确地将参数传递给Go Exec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆