drk49438 2012-04-30 00:30
浏览 24
已采纳

如何启动Go中非文件的进程(例如,打开网页)

I want to open a web browser:

c, err := exec.Command("http://localhost:4001").Output()
if err != nil {
    fmt.Printf("ERROR: %v, %v
",err,c)
} else {
    fmt.Printf("OK:%v
",c)
}

and I get the error

ERROR: exec: "http://localhost:4001": file does not exist

Edit: What I want to achieve is the same as in Windows and C# when you do:

Process.Start("http://localhost:4001")

With it a new instance of your default browser will launch showing the URL

  • 写回答

6条回答 默认 最新

  • duanpo7282 2012-04-30 08:53
    关注

    "http://localhost:4001/" is a URL, it can not be executed, but you can execute a web browser (e.g. firefox) and pass the URL as first argument.

    On Windows, OS X, and Linux helper programs exist which can be used to start the default web browser. I guess there is a similar thing for FreeBSD and Android, but I am not sure about it. The following snippet should work on Windows, OS X, and most Linux distros:

    var err error
    switch runtime.GOOS {
    case "linux":
        err = exec.Command("xdg-open", "http://localhost:4001/").Start()
    case "windows", "darwin":
        err = exec.Command("open", "http://localhost:4001/").Start()
    default:
        err = fmt.Errorf("unsupported platform")
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效