Golang URL Shortener
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:
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://firstname.lastname@example.org#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.
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.