Erhan Yakut Software Developer @Binalyze | Founder @Passwall | Golang Enthusiast | Open Sorcerer

Iterate, Slice, and Deduplicate: Go’s New Tools for Cleaner Code

1 min read

Go keeps evolving — not through radical changes, but through small, elegant additions that make everyday programming smoother. Three of the newest additions to the standard library — iter, slices, and unique — are perfect examples of this evolution. Together, they bring safer, more expressive, and more composable ways to work with data, making Go code both simpler and more powerful.


The iter Package — Functional Iteration, Go Style

For years, Go developers relied on slices, channels, or custom closures for iteration. The iter package formalizes that pattern — introducing typed iterator functions that integrate seamlessly with Go’s for … range syntax.


func Numbers() iter.Seq[int] {
    return func(yield func(int) bool) {
        for i := 0; i < 3; i++ {
            if !yield(i) {
                return
            }
        }
    }
}

for n := range Numbers() {
    fmt.Println(n)
}

Why it matters:

  • Enables lazy, composable iteration (no need to build full slices in memory).
  • Works naturally with range.
  • Provides a new foundation for streaming or pipeline-like APIs in Go.

The slices Package — Safe, Generic Utilities for Everyday Work

Every Go developer has written some variation of “find,” “sort,” or “delete from slice.” Now, these operations are part of the language’s toolkit — type-safe, generic, and built right in.


import "slices"

nums := []int{3, 1, 2}
slices.Sort(nums)
fmt.Println(nums) // [1 2 3]

if slices.Contains(nums, 2) {
    fmt.Println("Found 2!")
}

copy := slices.Clone(nums)

Highlights:

  • Sort, Contains, Insert, Delete, and Clone — all type-safe.
  • Cleaner syntax than sort.Slice or manual loops.
  • No external dependencies like x/exp/slices.

Why it matters: Fewer bugs, less boilerplate, and a standard way to handle the most common operations on slices.


The unique Package — Deduplication Made Simple

Go now includes a lightweight utility for deduplicating sorted slices — a deceptively small feature that simplifies many real-world workflows.


import (
    "slices"
    "unique"
    "fmt"
)

names := []string{"bob", "bob", "alice", "carol", "carol"}
slices.Sort(names)
n := unique.Strings(names)
fmt.Println(names[:n]) // [alice bob carol]

How it works:

  • Removes duplicates in place (no extra allocation).
  • Returns the index up to which the elements are unique.
  • Supports multiple types (Ints, Strings, etc.).

Why it matters: Great for cleaning datasets, logs, or identifiers — without hand-written deduplication loops.


Composing Them Together

These new tools are designed to compose beautifully:


seq := func(yield func(int) bool) {
    for i := 0; i < 10; i++ {
        yield(i % 3)
    }
}

nums := slices.Collect(seq)
slices.Sort(nums)
n := unique.Ints(nums)
fmt.Println(nums[:n]) // [0 1 2]

Here, we:

  1. Generate numbers lazily with an iterator.
  2. Collect them into a slice.
  3. Sort and deduplicate — all with pure standard library code.

✍️ Final Thoughts

The addition of iter, slices, and unique doesn’t reinvent Go — it refines it. They make iteration more expressive, collections safer, and data handling cleaner, all while staying true to Go’s philosophy of simplicity and clarity.

If you write Go regularly, take a weekend to refactor some loops with iter.Seq and replace your helper utilities with slices and unique. You’ll end up with code that’s more idiomatic, maintainable, and simply Go-ier.

Erhan Yakut Software Developer @Binalyze | Founder @Passwall | Golang Enthusiast | Open Sorcerer

Designing an Endpoint Management Platform

Managing endpoints in enterprise environments is a recurring challenge. As organizations grow, manual operations become unmanageable, visibility decreases, and responding to incidents takes longer...
Erhan Yakut
2 min read

Retrieving the Correct Hostname on macOS

When dealing with macOS systems, retrieving the correct hostname can be tricky, especially in environments where DHCP and reverse lookup mechanisms are in play....
Erhan Yakut
1 min read

Leave a Reply

Your email address will not be published. Required fields are marked *