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
+7.3k SSL : How to check if current certificate is sha1 or sha2 from command line
+8k Golang : Number guessing game with user input verification example
+13.7k Golang: Pad right or print ending(suffix) zero or spaces in fmt.Printf example
+7.9k Prevent Write failed: Broken pipe problem during ssh session with screen command
+10.8k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example
+8.6k Golang : Sort lines of text example
+22.2k Generate checksum for a file in Go
+12.3k Android Studio : Highlight ImageButton when pressed on example
+24.1k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+22.4k Golang : untar or extract tar ball archive example
+15.8k Golang : Get sub string example