My first Golang web project is online

It is true: I fell in love with Go, not because I love Google and his products, but because it really fits my ideology of simplicity and power in a programming language. I started experimenting with the language and thank to his web-oriented approach I quickly came up with one of the simplest single task web application I could write: a URL shortener.

What is a URL shortener? It’s a service that will give you a short link for a long URL.

Why should I use it? A short URL is easier to remember and to copy and paste, it lets you write more on social media where characters are limited (Twitter).

Why writing one when there are plenty of them already? Other shortener have an expiration date on short links, 4pr.es doesn’t!

Take a look at the code and you’ll see it is very simple: it takes a URL as text input filed of a form

short, err := createUrl(req.FormValue("url"))

generates a random string of 6 charachters, checking that the string is not in the database

...
for urlPresent(coder.Shrt) {
        coder.Shrt = shorten(coder.Length)
}
...
func shorten(c uint) string {
    rand.Seed(time.Now().UnixNano())
    b := make([]rune, c)
    for i := range b {
        b[i] = letters[rand.Intn(len(letters))]
    }
    return string(b)
}

and keep the URL – string association in the database for further redirection; once the random short URL is visited the client gets redirected to the origianl long URL!

...
err := getUrl(params["short"], w, req)
...
func getUrl(short string, w http.ResponseWriter, req *http.Request) error {
    var redir string
    err := db.QueryRow("SELECT url FROM urls WHERE short = ?", short).Scan(&redir)
    if err != nil {
        return err
    }
    http.Redirect(w, req, redir, 301)
    return nil
}

So please try it and if you have any suggestion to increase performance or security please let me know!

Cheers