-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinternet.go
44 lines (37 loc) · 931 Bytes
/
internet.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
package internet
import (
"bytes"
"io"
"github.com/sereiner/faker/person"
"github.com/sereiner/faker"
"github.com/sereiner/faker/base"
"github.com/valyala/fasttemplate"
)
var template *fasttemplate.Template
func init() {
template = fasttemplate.New(base.RandomElement(emailFormats), "{{", "}}")
}
// Email 生成一个 email
func Email() string {
format := base.RandomElement(emailFormats)
return formatString(format)
}
func formatString(format string) string {
buf := faker.BufferPool.Get().(*bytes.Buffer)
buf.Reset()
defer faker.BufferPool.Put(buf)
template.Reset(format, "{{", "}}")
_, err := template.ExecuteFunc(buf, func(w io.Writer, tag string) (int, error) {
switch tag {
case "userName":
return w.Write([]byte(person.LastName()))
case "domainName":
return w.Write([]byte(base.RandomElement(freeEmailDomain)))
}
return 0, nil
})
if err == nil {
return buf.String()
}
return ""
}