It's probably fair to say we have all  used a shortened url at least once in our lifetime, these small utilities services are quite helpful in reducing noise when sharing links.

One of the most popular shorteners out there is https://tinyurl.com/  where you can input a very long URL address and it can output a much shorter version of it much easier to share around.

In this series of article, we will be exploring how to write a url shortener in Go programming language and we will be using Redis as store mechanism for super fast data retrieval in the implementation.

This article should give a good understanding of fundamental building blocks of url shortener service and should should provide a good introduction to using Redis for those who haven't yet get to use it in a Go app setup.

I. 1. Project Setup 🛠

Let's setup the project and install all dependencies that will be needed along the way during the project build-up.

  • Initialize the go project, please make sure you have Go 1.11+ installed in your system.
$ go mod init github.com/your-username/go-url-shortener
  • Create main.go file and add the code below for checking the setup.
package main

import "fmt"

func main() {
	fmt.Printf("Hello Go URL Shortener !🚀")
}

Run go run main.go in the terminal inside the project directory, the code above should output : "Hello Go URL Shortener !🚀 ".  Bravo ! The init setup was successful.

  • Installing project dependencies.
$ go get github.com/go-redis/redis/v8
 
$ go get -u github.com/gin-gonic/gin
Note : In the next steps of this tutorial, you will need Redis installed on your computer.
If Redis is not yet installed on your computer, you can download it from this link here and follow the instructions for installation with respect to your operating system.

I. 2. Igniting The Web Server ☄️

At this step, we are ready to launch the web server for first time, and return some data from its API endpoint. The goal is to check if our initial setup went well.

package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	r.GET("/", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "Hey Go URL Shortener !",
		})
	})

	err := r.Run(":9808")
	if err != nil {
		panic(fmt.Sprintf("Failed to start the web server - Error: %v", err))
	}
}
Update the main.go file to reflect these changes 

Run again the main.go file and go to http://localhost:9808/ in the browser or any rest client tool, it should output a result like the one below 👇

{
  "message": "Hey Go URL Shortener !"
}

Awesome that you have reached at this point, if you got the json response above at that endpoint, it should mean our setup was done successfully 👏

I. 3. Conclucion & Recap.


Now  we have reached the end of PART I in this tutorial series, where we mainly focused on the initial setup and first smoke tests.
Stay tuned the next part of the series which will be coming very soon and should be shared on all the channels, on my twitter account mainly, so make sure you follow to stay update 😇
You can find the fully completed project here 👈


Ciao ! 👋