Skip to content

Instantly share code, notes, and snippets.

@hyg
Created June 19, 2014 09:36
Show Gist options
  • Select an option

  • Save hyg/9c4afcd91fe24316cbf0 to your computer and use it in GitHub Desktop.

Select an option

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)
}
}
@modyuan

modyuan commented Apr 7, 2018

Copy link
Copy Markdown

it works ! very good !

@jonatjano

Copy link
Copy Markdown

@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

@EchoofthePast

Copy link
Copy Markdown

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

@donething

donething commented Jan 19, 2019

Copy link
Copy Markdown

谢谢,有效。

@liran

liran commented May 23, 2019

Copy link
Copy Markdown

Pretty Cool.

@tharinduwijewardane

Copy link
Copy Markdown

Is there a way to programmatically close the opened browser?

@billyct

billyct commented Sep 20, 2019

Copy link
Copy Markdown

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

tharinduwijewardane commented Sep 27, 2019

Copy link
Copy Markdown

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

billyct commented Sep 27, 2019

Copy link
Copy Markdown

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
Copy Markdown

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

@tharinduwijewardane

Copy link
Copy Markdown

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

@akshaybharambe14

Copy link
Copy Markdown

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

@gocs

gocs commented Apr 17, 2020

Copy link
Copy Markdown

this doesn't work on playground

@BastiCoding

Copy link
Copy Markdown

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

lifeng1992 commented Sep 13, 2020

Copy link
Copy Markdown

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

omgupta1608 commented Jan 8, 2021

Copy link
Copy Markdown

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

gocs commented Mar 2, 2021

Copy link
Copy Markdown

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
Copy Markdown

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

Trisia commented Feb 23, 2022

Copy link
Copy Markdown

nice it work!

@amirhoseinjfri

Copy link
Copy Markdown

@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

ghost commented Jun 22, 2023

Copy link
Copy Markdown

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