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 33 34 35
|
package osx
import (
"os/exec"
"runtime"
)
// OpenDefault opens the specified file or URL with the default associated application.
//
// You may use it to open a web site:
// OpenDefault("https://google.com")
//
// Or open a file:
// OpenDefault("/home/bob/story.txt")
//
// Or open a folder in your default file manager:
// OpenDefault("/home/bob")
//
// For details, see https://stackoverflow.com/a/39324149/1705598
func OpenDefault(fileOrURL string) error {
var cmd string
var args []string
switch runtime.GOOS {
case "windows":
cmd = "cmd"
args = []string{"/c", "start"}
case "darwin":
cmd = "open"
default: // "linux", "freebsd", "openbsd", "netbsd"
cmd = "xdg-open"
}
args = append(args, fileOrURL)
return exec.Command(cmd, args...).Start()
}
|