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
+5k Javascript : How to loop over and parse JSON data?
+6.8k Golang : Use modern ciphers only in secure connection
+13.4k Golang : How to check if a file is hidden?
+5.5k Linux : Disable and enable IPv4 forwarding
+9.8k Golang : How to tokenize source code with text/scanner package?
+41k Golang : Convert string to array/slice
+7.2k Golang : Detect sample rate, channels or latency with PortAudio
+10.6k Golang : Fix go.exe is not compatible with the version of Windows you're running
+11.8k Golang : Flush and close file created by os.Create and bufio.NewWriter example
+36.8k Upload multiple files with Go
+19.3k Golang : Measure http.Get() execution time
+28.9k Golang : How to create new XML file ?