A URL Shortener Service using Go, Iris and Boltby@kataras
3,634 reads
3,634 reads

A URL Shortener Service using Go, Iris and Bolt

by Gerasimos MaropoulosMarch 15th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

If you follow the social media sites like <a href="" target="_blank">Twitter</a> and <a href="" target="_blank">Google+</a>, you’ll have noticed that rather than use the full URL we use a nice friendly shortened one like<a href="" target="_blank"></a>.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - A URL Shortener Service using Go, Iris and Bolt
Gerasimos Maropoulos HackerNoon profile picture

If you follow the social media sites like Twitter and Google+, you’ll have noticed that rather than use the full URL we use a nice friendly shortened one like

Wouldn’t be great to have your own shortened URLs inside your own domain?

Let’s review the basics first.

The Tools

Programming Languages are just tools for us, but we need a safe, fast and “cross-platform” programming language to power our service.

Go is a rapidly growing open source programming language designed for building simple, fast, and reliable software. Take a look here which great companies use Go to power their services.

Install the Go Programming Language

Extensive information about downloading & installing Go can be found here.

Maybe Windows or Mac OS X user?

The article does not contain an introduction to the language itself, if you’re a newcomer I recommend you to bookmark this article, learn the language’s fundamentals and come back later on.

The Dependencies

Many articles have been written, in the past, that lead users not to use a web framework because they’re “bad”. I have to tell you that there is no such a thing, it always depends on the (web) framework that you’re going to use.In production level, usually, we don’t have the time to code everything that we wanna use in the big company’s applications. In short terms: good frameworks are helpful tools for any developer, company or startup and bad are waste of time, clear and simple.

You’ll need only two required dependencies and one optional:

  • Iris , our web framework
  • Bolt (now bbolt), an embedded key/value database
  • UUID library, will help us to generate the short urls

Installing using go packages is a simple task, just open your terminal and execute the following commands:

$ go get -u$ go get -u$ go get -u

The Best Part

Good, if we’re all in the same page, it’s time to learn how we can create a URL Shortener server that will be easy to deploy and can be extended even more!

To create a short link, we generate a random string and use Bolt to store the original URL with our random string as the key. When a “GET ” request is issued to the shortened URL , we retrieve the original URL from Bolt. If such a value exists, we redirect, otherwise we respond with a corresponding message.

For the sake of simplicity let’s say that the project is located at $GOPATH/src/you/shortener directory and the package name is main.

Our front-end is ridiculous simple, it contains just an index page and its “style”, templates are located to ./templates folder and the style at ./resources/css folder.

Moving straight to the database level, we will create a simple implementation which will be able to save a shortened URL(key) and its original/full URL(value), retrieve a full URL based on the key and return the total number of all registered URLs to the database.

Let’s create our factory which will generate the shortened URLs for us!

We should create a main file which will connect and combine all the components to run an http server which will serve our small URL Shortener service.

And…. let’s build and run our application!

$ cd $GOPATH/src/$ go build # build$ ./url-shortener # run

BONUS section

Testing our apps is always a useful thing to do, therefore I’ll give you the first idea on how you can test these type of web applications with Iris, straight to the code below.

Running the test…

$ cd $GOPATH/src/$ go test -v # test

That’s all, pretty simple. Right?

Share your thoughts about this post and let me know what awesome apps you’re planing to build with Go + Iris!

Thanks for taking the time to read the whole article, I admire you for your patience :)

The full source code is located here.If you have any further questions please feel free to leave a comment below or ask here!


I have updated the source code and the article’s links at 31 August of 2018 in order to replace the ( archived) “” with the new from CoreOS based on the (released at 29 August of 2018)