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

前端之家收集整理的这篇文章主要介绍了如何启动Go中不是文件的进程(例如打开网页)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想打开一个网络浏览器:
  1. c,err := exec.Command("http://localhost:4001").Output()
  2. if err != nil {
  3. fmt.Printf("ERROR: %v,%v\n",err,c)
  4. } else {
  5. fmt.Printf("OK:%v\n",c)
  6. }

我收到错误

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

编辑:我想要实现的与Windows和C#中的相同:

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

使用它,默认浏览器的新实例将启动显示该URL

解决方法

“http:// localhost:4001 /”是一个URL,它不能执行,但可以执行一个Web浏览器(例如firefox),并将该URL作为第一个参数传递.

存在Windows,OS X和Linux帮助程序,可用于启动默认Web浏览器.我猜想FreeBSD和Android有类似的东西,但我不确定.以下片段应该适用于Windows,OS X和大多数Linux发行版:

  1. var err error
  2. switch runtime.GOOS {
  3. case "linux":
  4. err = exec.Command("xdg-open","http://localhost:4001/").Start()
  5. case "windows","darwin":
  6. err = exec.Command("open","http://localhost:4001/").Start()
  7. default:
  8. err = fmt.Errorf("unsupported platform")
  9. }

猜你在找的HTML相关文章