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

commented Apr 7, 2018

it works ! very good !

@jonatjano

This comment has been minimized.

Copy link

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

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

commented Jan 19, 2019

谢谢,有效。

@RanQiwu

This comment has been minimized.

Copy link

commented May 23, 2019

Pretty Cool.

@tharinduwijewardane

This comment has been minimized.

Copy link

commented Sep 2, 2019

Is there a way to programmatically close the opened browser?

@billyct

This comment has been minimized.

Copy link

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

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

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

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

commented Oct 8, 2019

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

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.