github.com/gudimz/urlShortener@v0.0.0-20230129195305-c8ee33059a67/internal/shorten/shorten.go (about)

     1  package shorten
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  const (
     8  	dictionary    = "abcdefghjkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"
     9  	lenDictionary = uint32(len(dictionary))
    10  )
    11  
    12  func GenerateShortenUrl(id uint32) string {
    13  	var (
    14  		builder strings.Builder
    15  		nums    []uint32
    16  	)
    17  
    18  	for id > 0 {
    19  		nums = append(nums, id%lenDictionary)
    20  		id /= lenDictionary
    21  	}
    22  
    23  	for _, num := range nums {
    24  		builder.WriteByte(dictionary[num])
    25  	}
    26  
    27  	return builder.String()
    28  }