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
+14.2k Golang : Send email with attachment(RFC2822) using Gmail API example
+4.6k PHP : Extract part of a string starting from the middle
+7.8k Golang : Sort words with first uppercase letter
+4.5k Mac OSX : Get disk partitions' size, type and name
+26.5k Golang : Force your program to run with root permissions
+14.3k Golang : Missing Bazaar command
+36.1k Golang : Validate IP address
+5.6k Golang : Fix opencv.LoadHaarClassifierCascade The node does not represent a user object error
+14k Golang : Convert IP version 6 address to integer or decimal number
+5.1k Golang : Generate Interleaved 2 inch by 5 inch barcode
+4.9k Golang : Check if a word is countable or not
+9.2k Golang : Terminate-stay-resident or daemonize your program?