Last active
August 29, 2016 20:36
-
-
Save asoseil/640e51e752ae843c632ff20ec3e8a9cd to your computer and use it in GitHub Desktop.
Go - open a URL in the default Web Browser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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