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
+9k Golang : List available AWS regions
+7.6k Golang : How to check if input string is a word?
+24.2k Golang : Convert uint value to string type
+5.8k Golang : When to use make or new?
+6k Mac OSX : Find large files by size
+14.6k Chrome : ERR_INSECURE_RESPONSE and allow Chrome browser to load insecure content
+8.2k Golang : How to use Gorilla webtoolkit context package properly
+25k Mac/Linux and Golang : Fix bind: address already in use error
+14.9k Golang : How to reverse elements order in map ?
+15.2k Golang : convert string or integer to big.Int type
+18.7k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+9.3k Golang : Text file editor (accept input from screen and save to file)