DEV Community

Cover image for Understanding Generics in Golang
Chinonso Amadi
Chinonso Amadi

Posted on • Updated on

Understanding Generics in Golang

Generics are a powerful feature in the Go programming language that allow you to write code that can work with multiple types. This can be useful for creating reusable components that can be used in different contexts, without having to write separate code for each type.

To use generics in Go, you first need to define a type parameter by specifying the letter T within angle brackets () after the name of the function, type, or method that you want to be generic. For example:

func mapValues[T](m map[string]T) []T {
    values := make([]T, 0, len(m))
    for _, v := range m {
        values = append(values, v)
    }
    return values
}


Enter fullscreen mode Exit fullscreen mode

In this example, the mapValues function is generic over the type T, which means that it can be used with maps that have values of any type.

To use the mapValues function, you can call it like any other function, but you need to specify the type that you want to use for T. You can do this by using the type keyword followed by the type that you want to use, within angle brackets. For example:


values := mapValues<int>(map[string]int{"a": 1, "b": 2, "c": 3})


Enter fullscreen mode Exit fullscreen mode

In this case, the mapValues function is called with a map that has values of type int, so T will be inferred to be int within the function. The resulting values slice will be a slice of int values.

You can also use generics with types by defining a type parameter within angle brackets after the type name. For example:


type Pair[T, U] struct {
    first  T
    second U
}


Enter fullscreen mode Exit fullscreen mode

This defines a Pair struct that is generic over two types, T and U. To create a Pair instance, you need to specify the types that you want to use for T and U within angle brackets. For example:

p := Pair[string, int]{"hello", 123}

Enter fullscreen mode Exit fullscreen mode

This creates a Pair instance with a first field of type string and a second field of type int.

Generics can also be used with methods. To define a generic method, you specify the type parameter within angle brackets after the func keyword and before the method name. For example:


func (p Pair[T, U]) String() string {
    return fmt.Sprintf("%v, %v", p.first, p.second)
}

Enter fullscreen mode Exit fullscreen mode

This defines a String method for the Pair type that is generic over the same two types, T and U. This method can be called on any Pair instance, and the types that were used for T and U when the instance was created will be used within the method.

In summary, generics in Go allow you to write code that can work with multiple types, without having to write separate code for each type. This can make your code more reusable and versatile, and can help you write more efficient and maintainable code.

Top comments (5)

Collapse
 
delta456 profile image
Swastik Baranwal

Your code is wrong. Go uses [T, U] not <T, U>.

Collapse
 
marcello_h profile image
Marcelloh
Collapse
 
mcosta74 profile image
Massimo Costa

Generics In are not defined using angle brakets but square one

Collapse
 
nonsoamadi10 profile image
Chinonso Amadi

Thanks everyone for the review, I have made needed changes

Collapse
 
birowo profile image
birowo

your code is wrong. try it on playground :
go.dev/play/p/2Ka9dxae-bs