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
+2.4k Golang : Characters limiter example
+4.4k Swift : Convert (cast) Character to Integer?
+10.6k Golang : Convert IP address string to long ( unsigned 32-bit integer )
+2.4k Golang : Get S3 or CloudFront object or file information
+7.9k Golang : Drop cookie to visitor's browser and http.SetCookie() example
+2.3k JQuery : Calling a function inside Jquery(document) block
+2.6k Golang : Combine slices of complex numbers and operation example
+10.7k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+2.8k Golang : Find relative luminance or color brightness
+3.4k Python : Fix SyntaxError: Non-ASCII character in file, but no encoding declared
+4.3k Golang : Random Rune generator
+3.3k Golang : Count leading or ending zeros(any item of interest) example