Skip to content

Instantly share code, notes, and snippets.

@sevkin
Last active March 11, 2025 12:00
Show Gist options
  • Save sevkin/9798d67b2cb9d07cb05f89f14ba682f8 to your computer and use it in GitHub Desktop.
Save sevkin/9798d67b2cb9d07cb05f89f14ba682f8 to your computer and use it in GitHub Desktop.
golang open url in default browser
// 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()
}
@onlysumitg
Copy link

onlysumitg commented Aug 15, 2024

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