Last active
March 11, 2025 12:00
-
-
Save sevkin/9798d67b2cb9d07cb05f89f14ba682f8 to your computer and use it in GitHub Desktop.
golang open url in default browser
This file contains hidden or 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
// https://stackoverflow.com/questions/39320371/how-start-web-server-to-open-page-in-browser-in-golang | |
// open opens the specified URL in the default browser of the user. | |
func open(url string) error { | |
var cmd string | |
var args []string | |
switch runtime.GOOS { | |
case "windows": | |
cmd = "cmd" | |
args = []string{"/c", "start"} | |
case "darwin": | |
cmd = "open" | |
default: // "linux", "freebsd", "openbsd", "netbsd" | |
cmd = "xdg-open" | |
} | |
args = append(args, url) | |
return exec.Command(cmd, args...).Start() | |
} |
I met error on windows. Url with query params was not opened correctly. I hope this source can help anyone Byron/open-rs#67
small fix
case "windows":
cmd = "cmd"
args = []string{"/c", "start" , url} // added missing "url" param
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍