Skip to Content

Project 5: Email Validator

Email regex and domain checker

Hello everyone, this is my 5th project on the journey of learning Golang. I want to share with you a simple program that I created to validate email addresses using regex and domain checker. This project might seem small, but it helped me learn some important concepts in Golang such as regular expressions and basic string manipulation. 

In this blog post, I will walk you through the steps I took to build this program, and I hope that it will be useful for you to learn Golang as well. You can find the source code in my GitHub here.

Terminal Input

Funny enough, this is my first project in Golang that will use a user input from terminal. From what I learn, there are two ways to doing this, below is the chunk of code that I will explain. Both of the example is not using any extra external package.

// Using Bufio
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
email := scanner.Text()
// Using fmt
var input string
fmt.Scan(&input)

In the first example, bufio.NewScanner creates a new scanner that reads from os.Stdin, which is the standard input (i.e. the terminal). The scanner.Scan() method reads the input until a newline character is encountered, if there is an input to read, this method will return true, otherwise false. The scanner.Text() method returns the input as a string.

For the second example, the program will read the input using fmt.Scan and store it in the input variable that has been declared before. Note that fmt.Scan expects a pointer to the variable where the input will be stored, so we use the "&" operator to get the address of the input variable.

The first example with bufio package is more common to use because it provides buffered I/O, which can help improve performance and reduce the number of system calls needed for input/output. This is especially useful when working with large amounts of data or when reading input from sources that may be slower, such as a network connection.

Regular Expression

RegEx, which is a sequence of characters that define a search pattern. It is a powerful tool used in many programming languages for pattern matching, text processing, and input validation. With this we can check if the user input is in fact an email format. Below is how I use the regexp in Golang.

// Regular expression pattern for validating email address
pattern := `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$` 

// Compile the pattern into a regular expression object
re := regexp.MustCompile(pattern) 

// Match the email address against the regular expression 
re.MatchString(email)

The MustCompile methode will create a regex object based on the pattern parameter. The object then used with MatchString method and will return true if the parameter matched with the pattern, otherwise false.

Checking Email Domain

First we need to crop the domain out of the input string. As we now, the domain is the string after "@" sign, so the domain will be after the "@" index to the last character. Below is the example code that I use.

atIndex := strings.LastIndex(email, "@")
result := email[atIndex+1:]

atIndex will store the "@" index, using the mehtod LastIndex to get. The result will store the email string with character index from the beginning of the string after "@" sign to the last character.

MX, SPF, and DMARC

After we get the domain name, we will check the MX, SPF, and DMARC attribute of the domain. With little googling, here is the meaning of each attribute.

  • MX (Mail Exchanger) record is a type of DNS record used to specify the mail server responsible for accepting email messages on behalf of a domain. It's used to route email messages to the appropriate email server.

  • SPF (Sender Policy Framework) is an email authentication mechanism used to verify the sender's identity and to prevent email spoofing. SPF records are published in the DNS records and they specify which mail servers are authorized to send email on behalf of a domain.

  • DMARC (Domain-based Message Authentication, Reporting, and Conformance) is an email authentication protocol that builds on SPF and DKIM (DomainKeys Identified Mail). It provides a way for email senders to specify policies for handling emails that fail authentication checks.

So a valid address not necessarily need to have all the three attribute, but a good one will. In Golang, amazingly there is already internal package that can be used to check these, and it is the "net" package. Below is the example code and each method that is in the package.

maxRecords, err := net.LookupMX(domain)
txtRecords, err := net.LookupTXT(domain) ​
dmarcRecords, err := net.LookupTXT("_dmarc." + domain)





Project 4: Go Slackbot
Slackbot for calculating age and uploading file