Generic implementation for interacting with various URL shortening services in Go.
As usual you can install the package by issuing:
$ go get github.com/subosito/shorturl
Shortening URL
ShortURL provides simple API to shorten a long URL, here's how:
// import "github.com/subosito/shorturl"
provider := "tinyurl"
u, err := shorturl.Shorten("http://example.com/", provider)
if err == nil {
fmt.Println(u)
}
Alternatively, you can initialize desired provider:
// import "github.com/subosito/shorturl/tinyurl"
s := tinyurl.New()
u, err := s.Shorten("http://example.com/")
if err == nil {
fmt.Println(u)
}
Expanding short URL
ShortURL also provides simple API to expand short URL into its original long URL:
// import "github.com/subosito/shorturl"
u, err := shorturl.Expand("http://bit.ly/13M3JX5")
if err == nil {
fmt.Println(u)
}
Package | Service | Enviroment Variables |
---|---|---|
bitly |
https://bitly.com/ | BITLY_ACCESS_TOKEN |
gggg |
http://gg.gg/ | - |
gitio |
https://git.io/ | - |
isgd |
https://is.gd/ | - |
shorl |
http://shorl.com/ | - |
tinyurl |
https://tinyurl.com/ | - |
Inspiration comes from Rubygem shorturl by @robbyrussell