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条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分