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 := "[email protected]"
valid := govalidator.IsEmail(testAddress)
fmt.Printf("%s is a valid email address : %v \n", testAddress, valid)
testAddress2 := "[email protected]"
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 := "[email protected]"
valid4 := govalidator.IsEmail(testAddress4)
fmt.Printf("%s is a valid email address : %v \n", testAddress4, valid4)
}
Output :
[email protected] is a valid email address : true
[email protected] is a valid email address : false
adamng()@socketloop.com is a valid email address : false
[email protected] 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
+3.4k Golang : Switch Redis database redis.NewClient
+8.9k Golang : Gonum standard normal random numbers example
+9.9k Golang : Print how to use flag for your application example
+10.9k Golang : Calculate Relative Strength Index(RSI) example
+29k Golang : JQuery AJAX post data to server and send data back to client example
+9.3k Golang : Read file with ioutil
+17.4k Convert JSON to CSV in Golang
+6.3k Elasticsearch : Shutdown a local node
+10.4k Golang : Underscore string example
+19k Golang : Execute shell command
+8.5k Golang : Take screen shot of browser with JQuery example
+6.1k Unix/Linux : Use netstat to find out IP addresses served by your website server