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.3k Golang : "https://" not allowed in import path
+9.9k Golang : Encrypt and decrypt data with x509 crypto
+4.3k Golang : List all packages and search for certain package
+6.3k Golang : How to get username from email address
+13.5k Golang : invalid character ',' looking for beginning of value
+24.5k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+5.9k Javascript : How to check a browser's Do Not Track status?
+8.6k Golang : Calculate Relative Strength Index(RSI) example
+6.8k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+4k Golang : What is StructTag and how to get StructTag's value?
+14.4k Golang : How to log each HTTP request to your web server?
+13.5k Golang : Execute terminal command to remote machine example