Created
June 19, 2014 09:36
-
Star
(166)
You must be signed in to star a gist -
Fork
(27)
You must be signed in to fork a gist
-
-
Save hyg/9c4afcd91fe24316cbf0 to your computer and use it in GitHub Desktop.
open browser in golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice it work!