PP

DevPortfolio

URL Shortener with Go

Dive into My Go Project That Shortens URLs and Generates Funny Links

URL Shortener with Go

URL Shortener with Go


In this article, we will walk through building a custom URL shortener application using the Go programming language. The application will provide an interface for users to input long URLs, which will be transformed into shorter, more manageable URLs. These short URLs will redirect users to the original long URLs when visited.

The application will also feature some customization options for the generated short URLs, such as including specific prefixes, custom prefixes, and supporting multiple URLs. The following sections will explain the code structure and functionality.

urlshortner-1
Prerequisites:
  • A basic understanding of the Go programming language

  • Go installed on your local machine

Getting Started:
  1. Create a new Go project directory and navigate into it.

  2. Create a

    main.go

    file and paste the provided Go code into it.

  3. Create an

    index.html

    file and paste the provided HTML code into it.

  4. Create a

    style.css

    file and paste the provided CSS code into it.


Code Structure and Functionality:
The main components of the application are as follows:
  • The main function sets up HTTP handlers for shortening URLs, redirecting URLs, and serving the CSS file. The application then listens on port 8080.

  • The shortenHandler function handles incoming POST requests, extracts the long URL from the form data, generates a short URL, and stores the long and short URLs in a map. It then returns the short URL to the user.

  • The redirectHandler function handles incoming GET requests, extracts the short URL from the request path, looks up the corresponding long URL in the map, and redirects the user to the long URL.

  • The generateShortUrl function generates a short URL by randomly selecting a prefix and appending a random string. Prefixes are stored in an array, and the random string is generated by the randomString function.

  • The randomString function generates a random string of a specified length using a predefined character set.


Running the Application:
  1. Open a terminal, navigate to the project directory,

    1. Run: go build shortener.go

    2. Followed by: ./shortener or doubleclick the shortener.exe file.

  2. to start the application.

  3. Open a web browser and navigate to http://localhost:8080. You should see the URL shortener form.

  4. Enter a long URL in the form and click "Shorten". The application will generate and display a short URL.

  5. Copy the short URL and paste it into a new browser tab. You will be redirected to the original long URL.

urlshortner-2
Conclusion:

In this article, we built a custom URL shortener application using the Go programming language. The application allows users to input long URLs and generates shorter, more manageable URLs that redirect to the original URLs when visited. With this foundation in place, you can further customize and extend the application to fit your specific needs, such as adding a database for storing URLs, implementing user authentication, or adding analytics for tracking URL usage.