Open opens a browser to the given URL. The terminal's open command is operating system dependent.
(url string)
| 9 | // Open opens a browser to the given URL. |
| 10 | // The terminal's open command is operating system dependent. |
| 11 | func Open(url string) error { |
| 12 | // Escape characters are not allowed by cmd/bash. |
| 13 | switch runtime.GOOS { |
| 14 | case "windows": |
| 15 | url = strings.Replace(url, "&", `^&`, -1) |
| 16 | default: |
| 17 | url = strings.Replace(url, "&", `\&`, -1) |
| 18 | } |
| 19 | |
| 20 | // The command to open the browser is OS-dependent. |
| 21 | var cmd *exec.Cmd |
| 22 | switch runtime.GOOS { |
| 23 | case "darwin": |
| 24 | cmd = exec.Command("open", url) |
| 25 | case "freebsd", "linux", "netbsd", "openbsd": |
| 26 | cmd = exec.Command("xdg-open", url) |
| 27 | case "windows": |
| 28 | cmd = exec.Command("cmd", "/c", "start", url) |
| 29 | } |
| 30 | |
| 31 | return cmd.Run() |
| 32 | } |