Making a URL Shortener using Go
It has been a long time since I wanted to learn to Go. Like everyone else, I struggled to think about what I could do. I started reading documentation aimlessly and eventually got bored of it. It had been installed on my laptop for months, but I did nothing with it.
Finally, I came across a post on X (fka Twitter) that urged developers to learn new skills by creating small projects. That inspired me to make something that could also facilitate learning.
I wanted to create a small service or tool that wouldn’t be ambitious but would give me enough ways to understand different aspects of Go. That is when I came across a GitHub repository where they had implemented a URL shortener using Go.
I started with the basics. I wrote my first main.go file and executed it. Even though the first command people run when learning a new language is print(“Hello World”), I ran fmt.Printf(“Hello World, I’m learning to Go”) — just to be different. At first, Go felt like a combination of C and Python to me. I had to import modules for basic I/O operations like I had done while learning C in my freshman year but Go also supported Python-esque string slicing.
One of my biggest revelations was how Go shared variables across packages. Since I’m used to writing variables using camelCase, I declared all my variables using camelCase format. The program refused to cooperate when I tried to pass one variable from my storage module to the shortener module. That is when I learned about how Go implements the package system, and the variable export is determined by whether the first character of that variable is uppercase.
Apart from that, I picked up on new things like Redis — Remote Dictionary Server, which large architectures use as a fast caching mechanism. I used Redis to store the shortened URL and the original URL represented in a dictionary structure, as the name suggests. I was surprised that some applications use Redis as their primary database to ensure speed as the system scales.
The shortening mechanism is based on rudimentary hashing and encoding algorithms like SHA256 and BASE58. The URL is first hashed along with a UserId to ensure that two users do not create a shortened URL pointing to two long URLs. Following the hashing, I used Base58 to encode the hash into a human-readable format. SHA256 was available within the Go crypto package, and I used another open-source package to implement base58 encoding.
My code can be found here: GitHub Repo.
My plan for this project is to create a docker container from this service, make a frontend UI for users to access the service, and shorten URLs! Although this project is nothing groundbreaking, I had a lot of fun picking up Go and learning its small intricacies, and I know this is just the tip of the iceberg!