go - 2 minutes read

A Beginners Overview to Go Generics

Golang has always been known for its simplicity and straightforwardness, but for a long time, it lacked one important feature: Generics. With the release of Go 1.18, generics were finally introduced, and they’ve been a game-changer for developers. We’ll explore what generics are and why they’re useful!

What Are Go Generics?

Generics lets you write functions that work with any data type. Instead of writing multiple functions to handle the same process for different types (like one for integers and one for strings), you can write one generic function that works for all types.

Imagine you had to write a function that finds the minimum value from a slice of integers, and later you need to do the same for a slice of floats. without generics, you'd have to write two versions of the same function. With generics, you write it once and reuse it for any type.

Why Are Generics Useful?

  1. Code Reusability: With generics, you can write functions or data structures that work with any type. This reduces code duplication.
  2. Type Safety: Even though you're writing a generic function, Go ensures that you’re using types correctly, preventing type errors at compile time.
  3. Cleaner Code: Without generics, you often had to use interfaces (which can sometimes be messy) or duplicate code for different types. Generics allow you to avoid that clutter.
  4. Improved Performance: By using generics, you eliminate the need for type assertion, which could lead to more efficient code in some cases.

How Do Generics Work?

Let's start with a basic example. A simple function that returns the smaller of two numbers without generics

func MinInt(a int, b int) int {
    if a < b {
        return a
    }
    return b
}

func MinFloat(a float64, b float64) float64 {
    if a < b {
        return a
    }
    return b
}

Urgh! You start to see the problem here we've wrote two functions to handle our types!

Now let's see what this looks like with generics

// T is a placeholder for any type that can be ordered (int, float64, etc.)
func Min[T comparable](a T, b T) T {
    if a < b {
        return a
    }
    return b
}

func main() {
    fmt.Println(Min(3, 5))        // Works with integers
    fmt.Println(Min(4.2, 2.7))    // Works with floats
}

Real-World Use Cases for Golang Generics

  1. Sorting Algorithms: You can write a generic sorting function that works with any sortable data type.
  2. Data Structures: Building reusable data structures (like stacks, queues, or linked lists) becomes easier and cleaner with generics.
  3. Error Handling: Functions that operate on different types of errors (custom error types, HTTP errors, etc.) can be written once and reused across the codebase.
  4. Database Access: When querying a database, you can write a generic function to parse results into different types (e.g., User, Order, etc.) without duplicating code.

My Takeaway

Generics lets you write one-size-fits-all functions and data structures, reducing duplication and increasing type safety. Whether you are dealing with numbers, strings, or custom data types, generics allow you to build more powerful and efficient programs. So, next time you find yourself writing the same logic for different types, consider using generics to simplify your life!

Bonus, Free Stuff!

I love giving back to the community as much as I can! So if you ever need to compare slices and find the differences, feel free to use a package I’ve open-sourced. It’s completely free to use, and I hope it helps! https://github.com/mtdevs28080617/go-slice-diff

Happy coding! 🎉

Spread the word!

© Copyright 2025. All rights reserved, MTDevs.