Creating and Publishing a Go Package: The emailcorrector Example

Tejaksha K
3 min readOct 24, 2024

In this tutorial, we’ll walk through the process of creating a Go package for validating and auto-correcting email addresses. We’ll build a simple package named emailcorrector, which will allow us to validate email formats, suggest corrections for common typos, and use the Levenshtein distance algorithm for minor mistakes. Finally, we will publish the package to GitHub so others can use it.

Prerequisites

  • Go installed on your machine (version 1.11 or later for modules).
  • A GitHub account to host your package.

Step 1: Set Up Your Go Environment

  1. Create a New Directory for your package:
mkdir emailcorrector
cd emailcorrector

2. Initialize a New Go Module:

go mod init github.com/tejaksha/emailcorrector

This command creates a go.mod file, which is used for managing module dependencies.

Step 2: Write the Package Code

  1. Create a New File named emailcorrector.go:
touch emailcorrector.go

2. Implement the Package Functions in emailcorrector.go:

package emailcorrector

import (
"errors"
"strings"
"unicode"
)

// ValidateEmail checks if the email format is valid.
func ValidateEmail(email string) error {
if !strings.Contains(email, "@") {
return errors.New("invalid email: missing '@'")
}
parts := strings.Split(email, "@")
if len(parts) != 2 || strings.TrimSpace(parts[0]) == "" || strings.TrimSpace(parts[1]) == "" {
return errors.New("invalid email: incorrect format")
}
return nil
}

// SuggestDomainCorrection suggests a correction for common domain typos.
func SuggestDomainCorrection(domain string) string {
domains := []string{"gmail.com", "yahoo.com", "outlook.com", "hotmail.com"}
for _, d := range domains {
if strings.Contains(d, domain) {
return d
}
}
return domain
}

// CorrectEmail attempts to correct an email address.
func CorrectEmail(email string) (string, error) {
if err := ValidateEmail(email); err != nil {
return "", err
}
parts := strings.Split(email, "@")
local := parts[0]
domain := SuggestDomainCorrection(parts[1])
return local + "@" + domain, nil
}

Step 3: Create the Main Application

  1. Create a New File named main.go:
touch main.go

2. Write the Example Code in main.go:

package main

import (
"fmt"
"github.com/tejaksha/emailcorrector"
)

func main() {
// Example of validating an email
email := "test@example.com"
if err := emailcorrector.ValidateEmail(email); err != nil {
fmt.Println("Invalid email:", err)
} else {
fmt.Println("Valid email:", email)
}

// Example of suggesting a domain correction
incorrectDomain := "gmial.com"
suggested := emailcorrector.SuggestDomainCorrection(incorrectDomain)
fmt.Printf("Did you mean: %s?\n", suggested)

// Example of correcting an email
incorrectEmail := "user@gmial.com"
corrected, err := emailcorrector.CorrectEmail(incorrectEmail)
if err != nil {
fmt.Println("Invalid email:", err)
} else {
fmt.Println("Corrected email:", corrected)
}
}

Step 4: Testing the Package

Run your application to test the package:

go run main.go

You should see output indicating whether the email is valid, along with any suggestions for corrections.

Step 5: Prepare for Publishing

  1. Create a README File:

Create a README.md file that documents your package. Here’s a simple example:

# emailcorrector

A Go package for validating and auto-correcting email addresses.

## Features

- Validate the format of an email address.
- Suggest corrections for common domain name typos (e.g., `gmial.com` → `gmail.com`).
- Use the Levenshtein distance algorithm to detect and correct minor mistakes.

## Installation

```bash
go get github.com/tejaksha/emailcorrector

Usage:

package main

import (
"fmt"
"github.com/tejaksha/emailcorrector"
)

func main() {
// Your example usage here
}

2. Commit Your Code:

git init
git add .
git commit -m "Initial commit of emailcorrector package"

Step 6: Create a Version Before Release

  1. Tagging Your Release:

To version your package before releasing it, you can create a Git tag. This is useful for keeping track of different versions of your package.

First, determine the version number you want to use (e.g., v1.0.0):

git tag v1.0.0

You can view the tags to confirm:

git tag

2. Push the Tag to GitHub:

To make the tag available on GitHub, push it using:

git push origin v1.0.0

3. Push Your Code to GitHub:

git remote add origin https://github.com/tejaksha/emailcorrector.git
git push -u origin master

Conclusion

Congratulations! You’ve created, versioned, and published a Go package for validating and correcting email addresses. By following these steps, you can share your work with the community and contribute to the rich ecosystem of Go libraries. Happy coding!

Refer My Github Repo:

--

--

Tejaksha K
Tejaksha K

Written by Tejaksha K

I'm a Full Stack Developer & Cloud Expert with experience in Google Cloud Platform & AWS.

No responses yet