If you follow the social media sites like and , you’ll have noticed that rather than use the full URL we use a nice friendly shortened one like . Twitter Google+ t.co/EvDSyOOPTH 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. is a open source programming language designed for building simple, fast, and reliable software. Take a look which great companies use Go to power their services. Go rapidly growing here Install the Go Programming Language Extensive information about downloading & installing Go can be found . here Maybe or user? Windows Mac OS X The article does not contain an introduction to the language itself, if you’re a newcomer I recommend you to bookmark this article, the language’s fundamentals and come back later on. learn 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: , our web framework Iris , an embedded key/value database Bolt (now bbolt) library, will help us to generate the short urls UUID Installing using go packages is a simple task, just open your terminal and execute the following commands: $ go get -u github.com/kataras/iris$ go get -u github.com/etcd-io/bbolt$ go get -u github.com/satori/go.uuid 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 directory and the package name is . $GOPATH/src/you/shortener main Our is ridiculous simple, it contains just an index page and its “style”, templates are located to folder and the style at folder. front-end ./templates ./resources/css Moving straight to the level, we will create a simple implementation which will be able to a shortened URL(key) and its original/full URL(value), a full URL based on the key and return the total number of all registered URLs to the database. database save retrieve Let’s create our which will the shortened URLs for us! factory generate We should create a which will connect and combine all the components to run an http server which will serve our small URL Shortener service. main file And…. let’s build and run our application! $ cd $GOPATH/src/github.com/myname/url-shortener$ go build # build$ ./url-shortener # run section BONUS 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. Testing Running the test… $ cd $GOPATH/src/github.com/myname/url-shortener$ 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 .If you have any further questions please feel free to leave a comment below or ask ! here here Update I have updated the source code and the article’s links at in order to replace the ( archived) “ ” with the new from CoreOS based on the (released at 29 August of 2018) 31 August of 2018 https://github.com/boltdb/bolt https://github.com/etcd-io/bbolt https://github.com/etcd-io/bbolt/releases/tag/v1.3.1-etcd.7
Share Your Thoughts