Building a URL Shortener in Go

Have you ever wondered how Bit.ly or TinyURL work? Today, we’re building our URL shortener in Golang!

By the end of this tutorial, you’ll have a fully working URL shortener that generates short links and redirects users. Let’s get started!

Before…


This content originally appeared on DEV Community and was authored by luthfisauqi17

Have you ever wondered how Bit.ly or TinyURL work? Today, we're building our URL shortener in Golang!

By the end of this tutorial, you'll have a fully working URL shortener that generates short links and redirects users. Let’s get started!

Before we dive into coding, let's understand how a URL shortener works:

  1. The user enters a long URL
  2. We generate a short code
  3. Save it in a memory or database
  4. When someone visits the short link, we redirect them

Step 1: Project Setup

First, create a new project and initialize Go modules.

mkdir go-url-shortener && cd go-url-shortener
go mod init github.com/yourusername/go-url-shortener
go get github.com/gin-gonic/gin

Now, open main.go and set up a simple Gin server.

package main

import (
    "crypto/rand"
    "encoding/base64"
    "github.com/gin-gonic/gin"
    "net/http"
)

// Map to store short URLs -> original URLs
var urlStore = make(map[string]string)

func main() {
    r := gin.Default()
    r.POST("/shorten", shortenURL)
    r.GET("/:short", redirectURL)

    r.Run(":8080") // Run on port 8080
}

This creates a basic Gin server. Now let’s add URL shortening!

Step 2: Generate Short URLs

Now, we need a function to generate a short random URL.

func generateShortURL() string {
    b := make([]byte, 6)
    rand.Read(b)
    return base64.URLEncoding.EncodeToString(b)[:6]
}

Step 3: Shorten URL API

Next, let’s create the /shorten endpoint that takes a long URL and returns a short one.

func shortenURL(c *gin.Context) {
    var req struct {
        OriginalURL string `json:"original_url"`
    }
    if err := c.BindJSON(&req); err != nil {
        c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
        return
    }

    shortCode := generateShortURL()

    urlStore[shortCode] = req.OriginalURL

    c.JSON(http.StatusOK, gin.H{
        "short_url": "http://localhost:8080/" + shortCode,
    })
}

This stores the original URL in a map and returns a short URL.
Now, let’s handle redirection!

Step 4: Redirect Short URLs

We need an endpoint that looks up the short URL and redirects users.

func redirectURL(c *gin.Context) {
    shortCode := c.Param("short")

    originalURL, exists := urlStore[shortCode]

    if !exists {
        c.JSON(http.StatusNotFound, gin.H{"error": "URL not found"})
        return
    }

    c.Redirect(http.StatusFound, originalURL)
}

Step 5: Testing the API

Let’s test this API using cURL!
Run the application by typing.

go run .

Shorten a URL

Request:

curl -X POST http://localhost:8080/shorten -H "Content-Type: application/json" -d '{"original_url": "https://google.com"}'

Response:

{
    "short_url": "http://localhost:8080/abc123"
}

Redirect (Visit the short URL)

curl -v http://localhost:8080/abc123

Full code: https://github.com/luthfisauqi17/go-url-shortner

There you go, that is how you build a URL Shortener using Golang. Thank you for reading, and have a nice day!


This content originally appeared on DEV Community and was authored by luthfisauqi17


Print Share Comment Cite Upload Translate Updates
APA

luthfisauqi17 | Sciencx (2025-02-16T12:43:21+00:00) Building a URL Shortener in Go. Retrieved from https://www.scien.cx/2025/02/16/building-a-url-shortener-in-go/

MLA
" » Building a URL Shortener in Go." luthfisauqi17 | Sciencx - Sunday February 16, 2025, https://www.scien.cx/2025/02/16/building-a-url-shortener-in-go/
HARVARD
luthfisauqi17 | Sciencx Sunday February 16, 2025 » Building a URL Shortener in Go., viewed ,<https://www.scien.cx/2025/02/16/building-a-url-shortener-in-go/>
VANCOUVER
luthfisauqi17 | Sciencx - » Building a URL Shortener in Go. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/02/16/building-a-url-shortener-in-go/
CHICAGO
" » Building a URL Shortener in Go." luthfisauqi17 | Sciencx - Accessed . https://www.scien.cx/2025/02/16/building-a-url-shortener-in-go/
IEEE
" » Building a URL Shortener in Go." luthfisauqi17 | Sciencx [Online]. Available: https://www.scien.cx/2025/02/16/building-a-url-shortener-in-go/. [Accessed: ]
rf:citation
» Building a URL Shortener in Go | luthfisauqi17 | Sciencx | https://www.scien.cx/2025/02/16/building-a-url-shortener-in-go/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.