urlshortener

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 10, 2026 License: BSD-3-Clause Imports: 3 Imported by: 0

README

Package urlshortener

The urlshortener package provides a simple mechanism for generating shortened URLs using MD5 hashing. It manages a mapping between original long URLs and their shortened counterparts.

Types

type URLShortener

URLShortener is a structure that serves as a registry for URL mappings.

type URLShortener struct {
    urls map[string]string
}

  • urls: A map where the keys represent the shortened URL strings and the values represent the original long URLs.

Functions

func Shorten

func shorten(input, domain string) string

shorten creates a shortened version of a given input URL using the specified domain.

  • Hashing: The function uses the MD5 algorithm to hash the input URL.
  • Encoding: The resulting hash is encoded into a hexadecimal string, and the first 7 characters are used as the unique identifier.
  • Collision Handling: If a generated URL already exists in the registry, the function re-hashes the generated URL to attempt to create a unique identifier.
  • Output: Returns a formatted string in the pattern https://{domain}/{hash_prefix}.

Example:

// Returns: "https://tinyurl.co.in/29fjfj"
shorten("amazon.com", "tinyurl.co.in") 

func Check (Internal)

func check(input string) bool

check is a helper function that determines whether a specific shortened URL already exists within the URLShortener map.

  • It returns true if the URL exists and false otherwise.

Made with coffee

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type URLShortener

type URLShortener struct {
	// contains filtered or unexported fields
}

URLShortener struct of original:shortened-url

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL