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
+6.9k Golang : Calculate how many weeks left to go in a given year
+19.4k Golang : Accept input from user with fmt.Scanf skipped white spaces and how to fix it
+6.8k Golang : Array mapping with Interface
+7.3k Golang : Getting Echo framework StartAutoTLS to work
+12.6k Golang : How to calculate the distance between two coordinates using Haversine formula
+7k Golang : Create zip/ePub file without compression(use Store algorithm)
+5.6k Golang : Generate multiplication table from an integer example
+8.4k Golang : Gorilla web tool kit schema example
+21k Golang : GORM create record or insert new record into database example
+18.2k Unmarshal/Load CSV record into struct in Go
+23.4k Golang : Upload to S3 with official aws-sdk-go package
+8.7k Golang : Intercept and compare HTTP response code example