-
-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathopen.go
More file actions
32 lines (28 loc) · 708 Bytes
/
open.go
File metadata and controls
32 lines (28 loc) · 708 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package browser
import (
"os/exec"
"runtime"
"strings"
)
// Open opens a browser to the given URL.
// The terminal's open command is operating system dependent.
func Open(url string) error {
// Escape characters are not allowed by cmd/bash.
switch runtime.GOOS {
case "windows":
url = strings.Replace(url, "&", `^&`, -1)
default:
url = strings.Replace(url, "&", `\&`, -1)
}
// The command to open the browser is OS-dependent.
var cmd *exec.Cmd
switch runtime.GOOS {
case "darwin":
cmd = exec.Command("open", url)
case "freebsd", "linux", "netbsd", "openbsd":
cmd = exec.Command("xdg-open", url)
case "windows":
cmd = exec.Command("cmd", "/c", "start", url)
}
return cmd.Run()
}