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)
}
Songzhibin

浙公网安备 33010602011771号