Skip to content

Instantly share code, notes, and snippets.

@hyg
Created June 19, 2014 09:36
Show Gist options
  • Save hyg/9c4afcd91fe24316cbf0 to your computer and use it in GitHub Desktop.
Save hyg/9c4afcd91fe24316cbf0 to your computer and use it in GitHub Desktop.
open browser in golang
func openbrowser(url string) {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
log.Fatal(err)
}
}
@Trisia
Copy link

Trisia commented Feb 23, 2022

nice it work!

@amirhoseinjfri
Copy link

@yuansushow idk if it existed when the gist was created but using https://godoc.org/github.com/pkg/browser seem more clean and should works for every golang supported distribution

thank you very much

@SinaMobasheri
Copy link

exec.Command("cmd", "/c", "start", <URL>).Start() for me it seem nicer and more casual than exec.Command("rundll32", "url.dll,FileProtocolHandler", <URL>) if anyone knows any downside to cmd and start approach, pleas letlet me know...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment