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
+11.1k Golang : Generate random elements without repetition or duplicate
+19.4k Golang : Display list of time zones with GMT
+8.1k Golang : What fmt.Println() can do and println() cannot do
+9.2k Golang : Simple histogram example
+7.5k Linux : How to fix Brother HL-1110 printing blank page problem
+5.9k Unix/Linux : Get reboot history or check when was the last reboot date
+10.9k Golang : Command line file upload program to server example
+6.2k Java : Human readable password generator
+6.9k Mac/Linux/Windows : Get CPU information from command line
+32.8k Golang : Regular Expression for alphanumeric and underscore
+13.2k Golang : Skip blank/empty lines in CSV file and trim whitespaces example
+20.8k Android Studio : AlertDialog and EditText to get user string input example