Go代码启动默认浏览器

package main

// 打开系统默认浏览器

import (
	"fmt"
	"os/exec"
	"runtime"
	"time"
)
// 不同平台启动指令不同
var commands = map[string]string{
	"windows": "explorer",
	"darwin":  "open",
	"linux":   "xdg-open",
}

func Open(uri string) error {
     // runtime.GOOS获取当前平台
	run, ok := commands[runtime.GOOS]
	if !ok {
		return fmt.Errorf("don't know how to open things on %s platform", runtime.GOOS)
	}

	cmd := exec.Command(run, uri)
	return cmd.Run()
}

func main() {
	 fmt.Println(exec.Command("start","http://127.0.0.1").Output())
	 time.Sleep(time.Minute)
}

  

posted @ 2020-03-13 16:42  Binb  阅读(789)  评论(0编辑  收藏  举报