-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemailgenerator.go
50 lines (45 loc) · 1.23 KB
/
emailgenerator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"errors"
"fmt"
"math/rand"
"time"
)
const (
defaultEmailCount = 100000
defaultDupePercent = 0.5
)
func generateEmails(emailCount int, dupePercentage float32) ([]string, error) {
var err error
if emailCount < 0 {
return []string{}, errors.New("emailCount must be greater than 0")
}
if dupePercentage > 0.9 || dupePercentage < 0 {
return []string{}, errors.New("dupePercentage must be a value between 0.0 and 0.9")
}
generateEmailCount := defaultEmailCount
if emailCount > 0 {
generateEmailCount = emailCount
}
dupeCount := 0
if dupePercentage > 0 && dupePercentage < 1 {
dupeCount = int(float32(generateEmailCount) * dupePercentage)
} else {
dupeCount = int(float32(generateEmailCount) * defaultDupePercent)
}
var emails []string
dupeCounter := 0
counter := 0
for len(emails) < generateEmailCount {
emails = append(emails, fmt.Sprint(counter, "[email protected]"))
for dupeCounter < dupeCount {
emails = append(emails, fmt.Sprint(counter, "[email protected]"))
dupeCounter++
}
counter++
}
// Shuffle our emails before returning them
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(emails), func(i, j int) { emails[i], emails[j] = emails[j], emails[i] })
return emails, err
}