I have checked the StackOverflow and couldn't find any question that answers how to validate email in Go Language.
After some research, I figured out and solved it as per my need.
I have this regex and Go function, which work fine:
import (
"fmt"
"regexp"
)
func main() {
fmt.Println(isEmailValid("test44@gmail.com")) // true
fmt.Println(isEmailValid("test$@gmail.com")) // true -- expected "false"
}
// isEmailValid checks if the email provided is valid by regex.
func isEmailValid(e string) bool {
emailRegex := regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
return emailRegex.MatchString(e)
}
The problem is that it accepts the special characters that I don't want. I tried to use some from other languages' "regex" expression, but it throws the error "unknown escape" in debug.
Could anyone give me a good regex or any fast solution (pkg) that works with GoLang?