Messaging service, adapters, refactoring
This commit is contained in:
32
adapters/randomstring/service.go
Normal file
32
adapters/randomstring/service.go
Normal file
@@ -0,0 +1,32 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user