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

This comment has been minimized.

Copy link

@modyuan modyuan commented Apr 7, 2018

it works ! very good !

@jonatjano

This comment has been minimized.

Copy link

@jonatjano 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

This comment has been minimized.

Copy link

@EchoofthePast 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

This comment has been minimized.

Copy link

@donething donething commented Jan 19, 2019

谢谢,有效。

@liran

This comment has been minimized.

Copy link

@liran liran commented May 23, 2019

Pretty Cool.

@tharinduwijewardane

This comment has been minimized.

Copy link

@tharinduwijewardane tharinduwijewardane commented Sep 2, 2019

Is there a way to programmatically close the opened browser?

@billyct

This comment has been minimized.

Copy link

@billyct 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

This comment has been minimized.

Copy link

@tharinduwijewardane 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

This comment has been minimized.

Copy link

@billyct 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

This comment has been minimized.

Copy link

@tharinduwijewardane tharinduwijewardane commented Oct 8, 2019

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

@tharinduwijewardane

This comment has been minimized.

Copy link

@tharinduwijewardane tharinduwijewardane commented Oct 8, 2019

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

@akshaybharambe14

This comment has been minimized.

Copy link

@akshaybharambe14 akshaybharambe14 commented Dec 5, 2019

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

@gocs

This comment has been minimized.

Copy link

@gocs gocs commented Apr 17, 2020

this doesn't work on playground

@loremcookie

This comment has been minimized.

Copy link

@loremcookie 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

This comment has been minimized.

Copy link

@lifeng1992 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.