Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

webbrowser

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.

Installation

go get github.com/uradical/webbrowser

Requires Go 1.18 or later.

Usage

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.

Accepted URLs

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.

Supported platforms

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.

Testing

go test ./...

License

Released under the MIT License.

About

Module to open a URL in default browser

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages