package utils import ( "math/rand" "sync" "time" ) const ( letterBytes = "0123456789" ) var mu sync.Mutex var generatedCodes = make(map[string]struct{}) func init() { rand.Seed(time.Now().UnixNano()) } func randomString(lens int) string { b := make([]byte, lens) for i := range b { b[i] = letterBytes[rand.Intn(len(letterBytes))] } return string(b) } func GenerateUniqueInvitationCode(lens int) string { var code string for { code = randomString(lens) mu.Lock() if _, ok := generatedCodes[code]; !ok { generatedCodes[code] = struct{}{} mu.Unlock() return code } mu.Unlock() } }