Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to build a URL Shortener with Go

Posted on Aug 11 Say you've created a Google doc or saw a link to a video and want to share it with your friend, but the link needs to be shorter. That's where URL shorteners help. Some even go a step further and allow you to customise the link.I often shorten and customise my links using bitly.In my previous article, I shared that I'll focus on two things - Go for backend development and Technical writing. This is to show I'm committed. What better way to learn than to apply and teach?We'll be building a simple Url Shortener with Go 😃, my fave programming language.Shall we?... Yes!The URL shortener will be a basic web application that handles HTTP requests, shortens URLs, and redirects users to the original URLs using unique shortened keys. We will us an in-memory map to store the relationships between the shortened keys and original URLs.Let's start by writing the code for our URL shortener. We'll break down the implementation into the following steps:Open your termial and create a new directory for the project, then initialize a Go module to manage dependencies.go mod init is to create a new go.mod file, which will be a module in the project folder.You can open up the folder from VS Code, or while in your terminal, enter code .Go is all about packages - they're the core of every Go program.Create a filed named main.go in the project directory, and enter the code below:In this initial step, we import the necessary packages for our Go program. We're using fmt for formatting and printing, math/rand for generating random keys, net/http for handling HTTP requests, and time for seeding the random number generator.We create a URLShortener struct to manage the mapping between original URLs and their shortened versions. This struct will have a urls field, which is a map with shortened keys as keys and original URLs as values.Here, we define a HandleShorten method for the URLShortener struct. This method handles POST requests, validates the input URL, generates a unique short key, and displays the original and shortened URLs in an HTML response along with an input form for users to enter more URLs.We create a generateShortKey function to generate unique short keys for the original URLs. This function generates a random alphanumeric key of length 6 characters, ensuring the uniqueness of the keys.In the main function, we create an instance of the URLShortener struct and set up the HTTP handlers for URL shortening and redirection. We listen on port 8081, and the server starts running.To run the URL shortener, follow these steps:The -o flag is used to specify the output binary's name, which is named go-url-short.Here's a GIF that shows the app running The full code can be accessed on this GitHub repo.Building a URL shortener with Go is a great way to learn about web development, HTTP handling, and working with data structures. Our simple implementation includes the essentials: URL shortening, redirection, and a basic HTML form for user interaction. This project can be a starting point for enhancing the user interface, adding analytics, and exploring more advanced features.Happy coding! 💻 Templates let you quickly answer FAQs or store snippets for re-use.Really fun project! Thanks for writing such an in-depth article :)Thank you @jd2r I'm glad it was fun! 😎 Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse Nevo David - Aug 1 Sh Raj - Jul 22 moayad khader - Jul 17 Valerie Kuzmina - Jul 21 Once suspended, envitab will not be able to comment or publish posts until their suspension is removed. Once unsuspended, envitab will be able to comment and publish posts again. Once unpublished, all posts by envitab will become hidden and only accessible to themselves. If envitab is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Ekemini Samuel. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag envitab: envitab consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging envitab will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

How to build a URL Shortener with Go

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×