Golang URL Shortener

Photo by Denys Nevozhai on Unsplash

Many companies find themselves creating a very large amount of documentation. With all this documentation they find themselves needing to shorten the long links that they’re sending everywhere. A very handy tool is a URL shortener, something that can take a large link and create a simple hash of the string that will allow a user to type in a smaller URL directly to a service that will redirect them to the larger URL destination.

So lets make a URL Shortener with an in-memory list of locations. Using the in-memory list makes things easier as we won’t be managing the data through another form of storage.

Start with a simple main file that can redirect us to a new location:

Running this with go run main.go we can connect to http://localhost:9000 and we’ll be direct to https://github.com where we wanted to go.

Now that we’ve learned how to redirect, let’s start handling some different locations. We’ll add two new handlers for “adding” a link and one for “getting” a link that we redirect to:

Now if we go to the new path https://localhost:9000/addLink?link=https://www.google.com we should be able to create a list of shortened URLs to help us redirect traffic.

Now…although this is handy, it’s not very useful for “finding” the links we’ve already created. Adding a HTTP handler at the root of the project that can be used to list all of our active links would be very helpful.

Let’s try something like this: we’ll go through our list of URLs and show the ones we’ve created and where they go. We can also improve our randomly generated short link with something like this:

Let’s take a look at this now. We can startup the service with go run main.go and take a look.

Now let’s see if we can get a list of the Shortlinks from the Home handler.

Using the shortlink you should find yourself at whatever location you’ve just added, although you’ll find that adding something like github.com or google.com might not work. When using something like https://github.com or https://google.com, you’ll find it does work though. This is because of the way the http.Redirect() works in the Go standard library, which requires a absolute path when attempting to reach a URL path outside your application. You can learn a bit more about how this works by looking at the std library documentation here: https://pkg.go.dev/net/http@go1.17.3#Redirect

Looking at the documentation and the code, we can see that there is a recommendation to use absolute path by combining with request path.
https://cs.opensource.google/go/go/+/refs/tags/go1.17.3:src/net/http/server.go;l=2096-2159

I want to make sure someone is giving me a full path since I’m not redirecting anywhere on a relative path. I’ll add a validation step that will check the front of the new link for a http or https within the string.

There you have it! A quick and easy URL shortener that can be improved over time with metrics or a local storage system instead of keeping all your links in memory.

To see the full code you can look here, otherwise if you are interested in learning about something else please let me know. I’m trying to add content with more coding examples in the future; if you like this change please let me know!

If you happened to like this exercise, maybe you’d be interested in learning about 2 Factor Auth for Golang as well.

--

--

Coffee Drinker, Chief Engineer of Dancing at Desk, Foodie, HyperText Transfer Operator, Overly energetic and active! 💻 ☕

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
John Stupka

Coffee Drinker, Chief Engineer of Dancing at Desk, Foodie, HyperText Transfer Operator, Overly energetic and active! 💻 ☕