browser is a dependency-free library for opening URLs (or files) in the most appropriate browser or assigned application, respectively. Based on the operating system and available executables, it tries to open URLs in the following sequence:
- command defined by the environment variable
$BROWSER
(if set) - operating system specific launcher
- macOS:
/usr/bin/open
- Windows:
cmd /c start
- otherwise:
xdg-open
(if environment variable$DISPLAY
is set)
- macOS:
- one of the following binaries from
$PATH
chrome
google-chrome
chromium
firefox
go get -d -u github.com/abc-inc/browser
import "github.com/abc-inc/browser"
...
if ok := browser.Open(url); !ok {
// something bad happened
}
go get -u github.com/abc-inc/browser
# open a URL in the default browser
browser http://localhost/
# open a local file in the default browser
browser file:///C:/Temp/report.html
# macOS: use Firefox even if another default web browser is set
BROWSER="open -a Firefox" browser http://localhost/file.zip
# download a file with wget or curl - whatever is available
BROWSER="wget:curl -o index.html" browser http://localhost/
Since browser leverages operating system specific launchers, it can also be used to open other files and directories:
# play audio file using the media player associated with the file type
browser file:///home/me/Music/track.mp3
# open a folder in Visual Studio Code or vi (must be installed separately)
BROWSER=code:vi browser file:///Users/me/dev/project
- github.com/pkg/browser: the de facto standard implementation
- github.com/hashicorp/terraform/command/webbrowser: encapsulates pkg/browser and provides a mock implementation for testing purposes.
Feature | abc-inc/browser | pkg/browser | hashicorp/terraform |
---|---|---|---|
Windows/Linux/macOS/OpenBSD | ☑️ | ☑️ | ☑️ |
FreeBSD/NetBSD (xdg-open) | ☑️ | ||
WSL support | ☑️ | ||
$BROWSER support | ☑️ | ||
open from io.Reader | ☑️ | ||
mock support | ☑️ | ||
custom Cmd (Args, Dir, Env, etc.) | ☑️ |