DEV Community

Cover image for Introduction to Go generics?
Ganesh Kumar
Ganesh Kumar

Posted on

Introduction to Go generics?

#go

Hello, I'm Ganesh Kumar. I'm working on git-lrc: a Git hook for Checking AI generated code.AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.

In my previous post, I explained how to work with an interface.

Now let's understand go generics.

Why is Go Generics needed?

Before I start explaining go generics, let me give an example of a simple code without using go generics.

Let's have a simple function for finding the index of the element.
We can see that both internal workings are the same, but the parameters and return variable types are different




package main

import "fmt"



func IndexInt (s []int,x int) int{

    for i, v := range s{
        if v == x{
            return i
 }

 } 
        return -1

}

func IndexString (s []string,x string) int{

    for i, v := range s{
        if v == x{
            return i
 }

 } 
        return -1

}


func main(){

    integer := []int{10,20,30,35,40,45}
    fmt.Println(IndexInt(integer, 35)) 

    strings := []string{"hello", "world"}
    fmt.Println(IndexString(strings, "example")) 
}
Enter fullscreen mode Exit fullscreen mode

As we can see, both are working in the same way.
Output:

gk@jarvis:~/exp/code/rd/go-exmaple$ go run main.go 
3
-1
Enter fullscreen mode Exit fullscreen mode

So, why can't we have a single function that does the same task with passing different types? This is how go generics play a major role in this.

What is Go Generics?

It is a feature that allows developers to write reusable, type-safe code that works with multiple types.

That means we can use Go generics to solve our problem.

In the example below, we will solve the above problem.

Using Go Generics

There are many constraints to using go generics.

For our case let's just use a comparable constraint.

let't create function Index with [T comparable] with using T as type.


package main

import "fmt"



func Index[T comparable] (s []T,x T) int{

    for i, v := range s{
        if v == x{
            return i
 }

 } 
        return -1

}


func main(){

    integer := []int{10,20,30,35,40,45}
    fmt.Println(Index(integer, 35)) 

    strings := []string{"hello", "world"}
    fmt.Println(Index(strings, "example")) 
}
Enter fullscreen mode Exit fullscreen mode

As we expected, we got the same results with one function.

Output

gk@jarvis:~/exp/code/rd/go-exmaple$ go run main.go 
3
-1
Enter fullscreen mode Exit fullscreen mode

Conclusion

Go Generics was introduced recently we might be using it in a few cases where it is necessary.

git-lrc

πŸ‘‰ Check out: git-lrc
Any feedback or contributors are welcome! It’s online, open-source, and ready for anyone to use.
⭐ Star it on GitHub:

GitHub logo HexmosTech / git-lrc

Free, Unlimited AI Code Reviews That Run on Commit

git-lrc logo

git-lrc

Free, Unlimited AI Code Reviews That Run on Commit


git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.

git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.

See It In Action

See git-lrc catch serious security issues such as leaked credentials, expensive cloud operations, and sensitive material in log statements

git-lrc-intro-60s.mp4

Why

  • πŸ€– AI agents silently break things. Code removed. Logic changed. Edge cases gone. You won't notice until production.
  • πŸ” Catch it before it ships. AI-powered inline comments show you exactly what changed and what looks wrong.
  • πŸ” Build a habit, ship better code. Regular review β†’ fewer bugs β†’ more robust code β†’ better results in your team.
  • πŸ”— Why git? Git is universal. Every editor, every IDE, every AI…




Top comments (0)