我想打开一个网络浏览器:
- c,err := exec.Command("http://localhost:4001").Output()
- if err != nil {
- fmt.Printf("ERROR: %v,%v\n",err,c)
- } else {
- fmt.Printf("OK:%v\n",c)
- }
我收到错误
- ERROR: exec: "http://localhost:4001": file does not exist
编辑:我想要实现的与Windows和C#中的相同:
- Process.Start("http://localhost:4001")
使用它,默认浏览器的新实例将启动显示该URL
解决方法
“http:// localhost:4001 /”是一个URL,它不能执行,但可以执行一个Web浏览器(例如firefox),并将该URL作为第一个参数传递.
存在Windows,OS X和Linux帮助程序,可用于启动默认Web浏览器.我猜想FreeBSD和Android有类似的东西,但我不确定.以下片段应该适用于Windows,OS X和大多数Linux发行版:
- 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")
- }