go
49 lines · 8 steps
Normalizing and deduping email addresses in Go
A small package that parses, validates, and canonicalizes email addresses, then collapses a list down to unique entries.
Explained by
highlit
1package email
2
3import (
4 "errors"
5 "net/mail"
6 "strings"
7)
8
9var ErrInvalidAddress = errors.New("invalid email address")
10
11func Normalize(raw string) (string, error) {
12 addr, err := mail.ParseAddress(strings.TrimSpace(raw))
13 if err != nil {
14 return "", ErrInvalidAddress
15 }
16
17 at := strings.LastIndex(addr.Address, "@")
18 if at <= 0 || at == len(addr.Address)-1 {
19 return "", ErrInvalidAddress
20 }
21
22 local := addr.Address[:at]
23 domain := strings.ToLower(addr.Address[at+1:])
24
25 if strings.Contains(domain, "..") || strings.HasPrefix(domain, ".") {
26 return "", ErrInvalidAddress
27 }
28
29 return local + "@" + domain, nil
30}
31
32func NormalizeAll(raws []string) ([]string, error) {
33 seen := make(map[string]struct{}, len(raws))
34 out := make([]string, 0, len(raws))
35
36 for _, raw := range raws {
37 norm, err := Normalize(raw)
38 if err != nil {
39 return nil, err
40 }
41 if _, dup := seen[norm]; dup {
42 continue
43 }
44 seen[norm] = struct{}{}
45 out = append(out, norm)
46 }
47
48 return out, nil
49}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Lean on the standard library's mail.ParseAddress before layering on your own domain-specific checks.
- 2A single sentinel error lets callers compare with errors.Is instead of parsing message strings.
- 3A map with an empty-struct value is Go's idiomatic zero-cost set for deduplication.
Related explainers
javascript
const express = require('express'); const multer = require('multer'); const path = require('path'); const crypto = require('crypto');
Safe image uploads with Multer in Express
file-upload
multer
validation
Intermediate
7 steps
go
type PostCursor struct { CreatedAt time.Time ID int64 }
Keyset pagination with cursors in Go
pagination
keyset-cursor
database
Intermediate
8 steps
php
<?php namespace App\Http\Requests;
A validated date-range value object in PHP
value-object
validation
immutability
Intermediate
7 steps
rust
use std::collections::VecDeque; #[derive(Debug)] pub struct Hunk {
Applying a diff hunk in Rust
enums
error-handling
pattern-matching
Intermediate
8 steps
go
package config import ( "fmt"
Parsing timeout config in Go
configuration
validation
error-wrapping
Intermediate
7 steps
java
@Configuration public class OrderConsumerConfig { @Bean
Wiring a resilient Kafka consumer in Spring
kafka
deserialization
error-handling
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/normalizing-and-deduping-email-addresses-in-go-explained-go-5813/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.