Skip to content

Instantly share code, notes, and snippets.

@asoseil
Last active August 29, 2016 20:36
Show Gist options
  • Save asoseil/640e51e752ae843c632ff20ec3e8a9cd to your computer and use it in GitHub Desktop.
Save asoseil/640e51e752ae843c632ff20ec3e8a9cd to your computer and use it in GitHub Desktop.
Go - open a URL in the default Web Browser
package main
import (
"runtime"
"errors"
"bytes"
"os/exec"
"io"
)
var cli map[string]string
var stderr bytes.Buffer
func init() {
cli = map[string]string{
"windows": "start",
"darwin": "open",
"linux": "xdg-open",
}
}
func Open(urls ...string) (io.Writer, error) {
if open, err := cli[runtime.GOOS]; !err {
return nil, errors.New("This operating system is not supported.")
}else {
for _, value := range urls {
cmd := exec.Command(open, value)
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return cmd.Stderr, err
}
}
}
return nil, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment