Easy way to open an io.ReadCloser from a local file and URL.
$ go get -u github.com/utahta/go-openuri
import "github.com/utahta/go-openuri"
Open the file.
o, err := openuri.Open("/path/to/file")
Open the URL.
o, err := openuri.Open("http://localhost")
with Google App Engine
o, err := openuri.Open("http://localhost", openuri.WithHTTPClient(urlfetch.Client(ctx)))
package main
import (
"log"
"io/ioutil"
"github.com/utahta/go-openuri"
)
func main() {
//
// Open a local file
//
o, err := openuri.Open("/path/to/file")
if err != nil {
log.Fatal(err)
}
defer o.Close()
b, _ := ioutil.ReadAll(o)
log.Println(string(b))
//
// Open URL
//
o, err = openuri.Open("http://localhost")
if err != nil {
log.Fatal(err)
}
defer o.Close()
b, _ = ioutil.ReadAll(o)
log.Println(string(b))
}
- Fork it ( https://github.com/utahta/go-openuri/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request