/
utils.go
59 lines (48 loc) · 1.21 KB
/
utils.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
51
52
53
54
55
56
57
58
59
package common
import (
"fmt"
"net/mail"
"time"
)
//Checks whether string has special characters
func HasSpecials(text string) bool {
for i := 0; i < len(text); i++ {
switch c := text[i]; c {
case '(', ')', '<', '>', '[', ']', ':', ';', '@', '\\', ',', '.', '"':
return true
}
}
return false
}
//Check whether a string is present in string array
func SearchString(strArray []string, value string) bool {
for _, val := range strArray {
if val == value {
return true
}
}
return false
}
//Add an address to an address array
func AddStrToUniqueList(addrArray []string, addr string) []string {
if SearchString(addrArray, addr) {
return addrArray
}
return append(addrArray, addr)
}
//Check for a valid email address
func ParseAddress(addr string) (string, error) {
parsedAddr, err := mail.ParseAddress(addr)
if err != nil {
return "", fmt.Errorf("m-mail: invalid address %q: %v", addr, err)
}
return parsedAddr.Address, nil
}
//Return formated address for host and port
func HostPortAddr(host string, port int) string {
return fmt.Sprintf("%s:%d", host, port)
}
// FormatDate formats a date as a valid RFC 5322 date.
func FormatDate(date time.Time) string {
return date.Format(time.RFC1123Z)
}