33 lines
490 B
Go
33 lines
490 B
Go
package randomstring
|
|
|
|
import (
|
|
"context"
|
|
"math/rand"
|
|
)
|
|
|
|
const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
|
|
|
|
type Service struct {
|
|
chars []rune
|
|
length int
|
|
size uint
|
|
}
|
|
|
|
func New(size uint) *Service {
|
|
return &Service{
|
|
chars: []rune(chars),
|
|
length: len([]rune(chars)),
|
|
size: size,
|
|
}
|
|
}
|
|
|
|
func (s *Service) NewString(context.Context) string {
|
|
res := make([]rune, s.size)
|
|
|
|
for i := uint(0); i < s.size; i++ {
|
|
res[i] = s.chars[rand.Intn(s.length)]
|
|
}
|
|
|
|
return string(res)
|
|
}
|