A tiny, dependency-free Go module that opens a URL in the platform's default
web browser. It shells out to the native launcher — open on macOS, xdg-open
on Linux and the BSDs, and rundll32 on Windows.
go get github.com/uradical/webbrowserRequires Go 1.18 or later.
package main
import (
"log"
"github.com/uradical/webbrowser"
)
func main() {
if err := webbrowser.Open("https://go.dev"); err != nil {
log.Fatal(err)
}
}To bound how long the launcher may take to start, use OpenContext:
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := webbrowser.OpenContext(ctx, "https://go.dev"); err != nil {
log.Fatal(err)
}Open returns as soon as the launcher process has started; it does not wait for
the browser window to appear.
Only absolute http, https, and mailto URLs are accepted. Anything else —
a relative path, an unknown scheme, or a value that could be mistaken for a
command-line flag (e.g. -g) — is rejected with an error rather than passed to
the launcher.
| GOOS | Launcher |
|---|---|
darwin |
open |
linux, freebsd, openbsd, netbsd, dragonfly, solaris, illumos |
xdg-open |
windows |
rundll32 url.dll,FileProtocolHandler |
Any other platform returns an "unsupported platform" error.
go test ./...Released under the MIT License.