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