Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
Copy link

modyuan commented Apr 7, 2018

it works ! very good !

@jonatjano
Copy link

jonatjano commented Apr 22, 2018

@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

EchoofthePast commented Aug 6, 2018

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

tharinduwijewardane commented Sep 2, 2019

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

tharinduwijewardane commented Oct 8, 2019

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

@tharinduwijewardane
Copy link

tharinduwijewardane commented Oct 8, 2019

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

@akshaybharambe14
Copy link

akshaybharambe14 commented Dec 5, 2019

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

loremcookie commented May 17, 2020

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

viswanathtadi commented Jul 1, 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?

Use http or https before urls

@Trisia
Copy link

Trisia commented Feb 23, 2022

nice it work!

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