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)
}
}
@omgupta1608
Copy link

omgupta1608 commented Jan 8, 2021

I don't know why, but running the above code opens file explorer instead of the browser on my computer.
OS: Windows 10 64bit
Any idea to solve this issue?

@gocs
Copy link

gocs commented Mar 2, 2021

I don't know why, but running the above code opens file explorer instead of the browser on my computer.
OS: Windows 10 64bit
Any idea to solve this issue?

typing google.com or any domain name in the file explorer should open your default browser.
maybe its is not a URL?

@viswanathtadi
Copy link

I don't know why, but running the above code opens file explorer instead of the browser on my computer.
OS: Windows 10 64bit
Any idea to solve this issue?

Use http or https before urls

@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