Introduction to Go generics?

1 4 26
calendar_todayschedule8 min read
— Originally published at dev.to

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")) 
}

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

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")) 
}

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

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…




🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

Understanding Anonymous Functions in Go

Ganesh Kumar - Mar 23

Understanding Closures in Go

Ganesh Kumar - Mar 23

Understanding Methods in Go

Ganesh Kumar - Mar 25

How to Connect a DHT11 to a NodeMCU v2 board?

Ganesh Kumar - Apr 14

What I’m Doing to Not Become Irrelevant

pachecoioverified - Feb 8
chevron_left
1.1k Points31 Badges
44Posts
5Comments
3Connections
I am tech enthusiast, IoT innovator, software developer.

Related Jobs

View all jobs →

Commenters (This Week)

1 comment
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!