-
-
Save hyg/9c4afcd91fe24316cbf0 to your computer and use it in GitHub Desktop.
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) | |
} | |
} |
@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
This nice piece of coding really helped me get across a difficult problem. I think it was brilliant.
Thank you
谢谢,有效。
Pretty Cool.
Is there a way to programmatically close the opened browser?
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)
}
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.
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)
}
@billyct thanks but the drawback is we need to know the location of the Chrome.
Another question, How can we set (authentication) header when opening an url?
exec.Command("cmd", "/C", "start", url).Run()
for windows.
this doesn't work on playground
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.
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.
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?
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?
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
nice it work!
@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
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...
it works ! very good !