func Encode(longURL string) (string, string, error) {
	// Parse the URL
	u, err := url.Parse(longURL)
	if err != nil {
		return "", "", err
	}
	// Check if the URL is not absolute - has not a valid scheme (http or https)
	if !u.IsAbs() {
		// Add a scheme
		u.Scheme = "https"
	}
	// Convert URL to string
	longURLAbs := u.String()
	log.Printf("Shortener, long_url = %s", longURLAbs)

	// MD5 Hash of URL
	longURLHash := md5.New()
	io.WriteString(longURLHash, longURLAbs)
	// Save hash to byte slice
	longURLHashBytes := longURLHash.Sum(nil)
	log.Printf("Shortener, long_url (Hash) = %x\n", longURLHashBytes)

	// Base64 (URL Safe) encode it
	longURLHashBytesB64Safe := base64.RawURLEncoding.EncodeToString(longURLHashBytes)
	log.Printf("Shortener, long_url (Hash+Base64) = %s", longURLHashBytesB64Safe)

	// Keep the first 7 characters
	shortURLB64Safe := longURLHashBytesB64Safe[:7]
	log.Printf("Shortener, short_url = %s", shortURLB64Safe)

	// Return short URL
	return longURLAbs, shortURLB64Safe, nil
}
// Encode takes a URL and converts it in a 7 character string
// using a hash function and base64 conversion.
//
// Returns the short URL and the long URL (absolute)
Encode takes a URL and converts it in a 7 character string
using a hash function and base64 conversion.

Returns the short URL and the long URL (absolute)
// Encode takes a URL and converts it in a 7 character string // using a hash function and base64 conversion . // // Returns the short URL and the long URL ( absolute )
func Encode(longURL string) (string, string, error) {
	u, err := url.Parse(longURL)
	if err != nil {
		return "", "", err
	}
	if !u.IsAbs() {
		u.Scheme = "https"
	}
	longURLAbs := u.String()
	log.Printf("Shortener, long_url = %s", longURLAbs)
	longURLHash := md5.New()
	io.WriteString(longURLHash, longURLAbs)
	longURLHashBytes := longURLHash.Sum(nil)
	log.Printf("Shortener, long_url (Hash) = %x\n", longURLHashBytes)
	longURLHashBytesB64Safe := base64.RawURLEncoding.EncodeToString(longURLHashBytes)
	log.Printf("Shortener, long_url (Hash+Base64) = %s", longURLHashBytesB64Safe)
	shortURLB64Safe := longURLHashBytesB64Safe[:7]
	log.Printf("Shortener, short_url = %s", shortURLB64Safe)
	return longURLAbs, shortURLB64Safe, nil
}
func Encode ( longURL string ) ( string , string , error ) { u , err := url . Parse ( longURL ) if err != nil { return " " , " " , err } if ! u . IsAbs ( ) { u . Scheme = " " } longURLAbs := u . String ( ) log . Printf ( " " , longURLAbs ) longURLHash := md5 . New ( ) io . WriteString ( longURLHash , longURLAbs ) longURLHashBytes := longURLHash . Sum ( nil ) log . Printf ( " \n " , longURLHashBytes ) longURLHashBytesB64Safe := base64 . RawURLEncoding . EncodeToString ( longURLHashBytes ) log . Printf ( " " , longURLHashBytesB64Safe ) shortURLB64Safe := longURLHashBytesB64Safe [ : 7 ] log . Printf ( " " , shortURLB64Safe ) return longURLAbs , shortURLB64Safe , nil }
Encode takes a URL and converts it in a 7 character string
using a hash function and base64 conversion.
Encode takes a URL and converts it in a 7 character string using a hash function and base64 conversion .
// Parse the URL // Check if the URL is not absolute - has not a valid scheme (http or https) // Add a scheme // Convert URL to string // MD5 Hash of URL // Save hash to byte slice // Base64 (URL Safe) encode it // Keep the first 7 characters // Return short URL