browser.go 461 B

123456789101112131415161718192021222324
  1. package utils
  2. import (
  3. "os/exec"
  4. "runtime"
  5. )
  6. // OpenBrowser opens the specified URL in the default browser of the user.
  7. func OpenBrowser(url string) error {
  8. var cmd string
  9. var args []string
  10. switch runtime.GOOS {
  11. case "windows":
  12. cmd = "cmd"
  13. args = []string{"/c", "start"}
  14. case "darwin":
  15. cmd = "open"
  16. default: // "linux", "freebsd", "openbsd", "netbsd"
  17. cmd = "xdg-open"
  18. }
  19. args = append(args, url)
  20. return exec.Command(cmd, args...).Start()
  21. }