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.5k Unix/Linux : Use netstat to find out IP addresses served by your website server
+8k Golang : Lock executable to a specific machine with unique hash of the machine
+6.7k Golang : Warp text string by number of characters or runes example
+11.9k Golang : Secure file deletion with wipe example
+5.8k Linux/Unix/PHP : Restart PHP-FPM
+29.2k Golang : Get first few and last few characters from string
+7.2k Golang : Validate credit card example
+17.6k Golang : Multi threading or run two processes or more example
+7.6k Golang : How to handle file size larger than available memory panic issue
+19.6k Golang : Example for DSA(Digital Signature Algorithm) package functions
+26.9k Golang : Convert file content into array of bytes
+6.2k Golang : Grab news article text and use NLP to get each paragraph's sentences