Go 1.18 released with Generics support

Go 1.18 isn now generally available. This release supports generics which a lot of us have been waiting for. This is still the very first version to support generics and I believe this feature will gain more support and updates in the upcoming features.

From the Go blog:

In Go 1.18, we’re introducing new support for generic code using parameterized types. Supporting generics has been Go’s most often requested feature, and we’re proud to deliver the generic support that the majority of users need today. Subsequent releases will provide additional support for some of the more complicated generic use cases. We encourage you to get to know this new feature using our generics tutorial, and to explore the best ways to use generics to optimize and simplify your code today. The release notes have more details about using generics in Go 1.18.

Go Tutorial does an excellent job explaining how to use generics. I think they did a good job defining this functionally. The code is so much readable.

// SumIntsOrFloats sums the values of map m. It supports both int64 and float64
// as types for map values.
func SumIntsOrFloats[K comparable, V int64 | float64](m map[K]V) V {
    var s V
    for _, v := range m {
        s += v
    }
    return s
}

The [K comparable, V int64 | float64] tells me that you can either send either int or float values to this function.

It makes me wonder if there are product managers for programming languages or not? And, if yes, would love to talk to someone about their experience.

Published by

Vandan

Photographer. Foodie. Human.