Golang : Validate email address
Problem :
You need a quick way to validate if a user has entered a valid email address without knowing how to write the regular expression to parse the email address.
Solution :
Use IsEmail
function from github.com/asaskevich/govalidator
package. For example :
package main
import (
"fmt"
"github.com/asaskevich/govalidator"
)
func main() {
testAddress := "adamng@socketloop.com"
valid := govalidator.IsEmail(testAddress)
fmt.Printf("%s is a valid email address : %v \n", testAddress, valid)
testAddress2 := ".adamng@socketloop.com"
valid2 := govalidator.IsEmail(testAddress2)
fmt.Printf("%s is a valid email address : %v \n", testAddress2, valid2)
testAddress3 := "adamng()@socketloop.com"
valid3 := govalidator.IsEmail(testAddress3)
fmt.Printf("%s is a valid email address : %v \n", testAddress3, valid3)
testAddress4 := "adamng_@socketloop.com"
valid4 := govalidator.IsEmail(testAddress4)
fmt.Printf("%s is a valid email address : %v \n", testAddress4, valid4)
}
Output :
adamng@socketloop.com is a valid email address : true
.adamng@socketloop.com is a valid email address : false
adamng()@socketloop.com is a valid email address : false
adamng_@socketloop.com is a valid email address : true
Reference :
See also : Golang : Send email and SMTP configuration example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+10.5k Golang : Get remaining text such as id or filename after last segment in URL path
+6.5k Golang : Gorrila set route name and get the current route name
+7.2k How to show different content from website server when AdBlock is detected?
+6.2k Golang : Use modern ciphers only in secure connection
+6.9k Golang : Handle Palindrome string with case sensitivity and unicode
+6.4k Golang : Individual and total number of words counter example
+16.1k Golang : Find smallest number in array
+6.7k Gogland : Where to put source code files in package directory for rookie
+6.3k Golang : Scanf function weird error in Windows
+6.8k Golang : Get today's weekday name and calculate target day distance example
+13k Golang : Recombine chunked files example
+7.7k Golang : Accept any number of function arguments with three dots(...)