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

This nice piece of coding really helped me get across a difficult problem. I think it was brilliant.
Thank you

@donething
Copy link

donething commented Jan 19, 2019

谢谢,有效。

@liran
Copy link

liran commented May 23, 2019

Pretty Cool.

@tharinduwijewardane
Copy link

Is there a way to programmatically close the opened browser?

@billyct
Copy link

billyct commented Sep 20, 2019

Is there a way to programmatically close the opened browser?

try

cmd := exec.Command("open", url)
cmd.Start()
if err := cmd.Process.Kill(); err != nil {
	log.Fatal("close the opened browser failed", err)
}

@tharinduwijewardane
Copy link

tharinduwijewardane commented Sep 27, 2019

Is there a way to programmatically close the opened browser?

try

cmd := exec.Command("open", url)
cmd.Start()
if err := cmd.Process.Kill(); err != nil {
	log.Fatal("close the opened browser failed", err)
}

I get a process already finished error. But the browser is still open.

@billyct
Copy link

billyct commented Sep 27, 2019

Is there a way to programmatically close the opened browser?

try

cmd := exec.Command("open", url)
cmd.Start()
if err := cmd.Process.Kill(); err != nil {
	log.Fatal("close the opened browser failed", err)
}

I get a process already finished error. But the browser is still open.

😞it seems not working with exec.Command("open", url)

the code below is working for me.

cmd := exec.Command(
	"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
	"https://google.com",
	"--user-data-dir=test-user-data",
)

if err := cmd.Start(); err != nil {
	log.Fatalln("can't open browser", err)
}

time.Sleep(5 * time.Second)

if err := cmd.Process.Kill(); err != nil {
	log.Fatal("close the opened browser failed", err)
}

@tharinduwijewardane
Copy link

@billyct thanks but the drawback is we need to know the location of the Chrome.

@tharinduwijewardane
Copy link

Another question, How can we set (authentication) header when opening an url?

@akshaybharambe14
Copy link

exec.Command("cmd", "/C", "start", url).Run() for windows.

@gocs
Copy link

gocs commented Apr 17, 2020

this doesn't work on playground

@loremcookie
Copy link

This doesn't work on the playground because it tries to open the browser on the machine it's running on. Try running the code on your own computer, then it will work.

@lifeng1992
Copy link

lifeng1992 commented Sep 13, 2020

Open a file from the command line with a specified application path

in darwin, open

  • By default, opens each file using the default application for that file.

@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