github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/misc/unique_str/unique_id_to_str.go (about)

     1  package unique_str
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  )
     7  
     8  var letterMap = map[byte]byte{
     9  	'0': 'a',
    10  	'1': 'b',
    11  	'2': 'c',
    12  	'3': 'd',
    13  	'4': 'e',
    14  	'5': 'f',
    15  	'6': 'g',
    16  	'7': 'h',
    17  	'8': 'i',
    18  	'9': 'j',
    19  }
    20  
    21  var letterArr = []byte("klmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
    22  
    23  func GenerateUniqueIDToStr(unique_id uint64, strLen int32) string {
    24  	unique_str_byte := []byte(fmt.Sprintf("%d", unique_id))
    25  
    26  	for i := 0; i < len(unique_str_byte); i = i + 2 {
    27  		temp := unique_str_byte[i]
    28  		unique_str_byte[i] = letterMap[temp]
    29  	}
    30  	for l := int32(len(unique_str_byte)); l < strLen; l++ {
    31  		unique_str_byte = append(unique_str_byte, letterArr[rand.Intn(len(letterArr))])
    32  	}
    33  	return string(unique_str_byte)
    34  }