DEV Community

Cover image for Reverse a Generic List in golang
Dsysd Dev
Dsysd Dev

Posted on

Reverse a Generic List in golang

In this post we create a simple program to reverse a list, not a specific type of list but a generic list.
Photo by Thomas Bormans on Unsplash
The Reverse Function

func reverse[T any](list []T) []T {
    for i, j := 0, len(list)-1; i < j; {
        list[i], list[j] = list[j], list[i]
        i++
        j--
    }
    return list
}
Enter fullscreen mode Exit fullscreen mode

This is a generic function called reverse that takes a slice of any type T as input and returns a reversed slice of the same type T.

func reverse[T any](list []T) : []T defines the reverse function. The [T any] syntax indicates that the function is generic and can work with slices of any type.

Inside the function, a loop is used to reverse the elements of the list. The loop iterates from the beginning (i) and end (j) of the slice toward the middle, swapping elements at positions i and j until they meet.

list[i], list[j] = list[j], list[i] swaps the elements at positions i and j. Pretty neat way to swap elements !!

i++ increments i, and j-- decrements j to move towards the center of the slice.

Once the loop completes, the function returns the reversed list.

The Main Function

package main

import "fmt"

func main() {
    l := []int{1, 2, 3, 4}
    l = reverse[int](l)
    fmt.Println(l)
}

In the main function, a slice of integers l is initialized with values {1, 2, 3, 4}.
Enter fullscreen mode Exit fullscreen mode

The reverse function is called with the slice l, and the returned result is assigned back to l.

Finally, fmt.Println(l) prints the reversed slice.

Video Format

If you prefer video format, then have a look at this short

https://www.youtube.com/shorts/gt3vRmCG75E

Conclusion

This code demonstrates how to use a generic function to reverse the elements of a slice in Go.

It’s worth noting that the usage of generics in Go is a feature introduced in Go 1.18 and won’t work with versions before that.
Claps Please!

If you found this article helpful I would appreciate some claps 👏👏👏👏, it motivates me to write more such useful articles in the future.

Follow for regular awesome content and insights.

Subscribe to my Youtube channel

Subscribe to my youtube channel if you are on the lookout for more such awesome content in video format.

https://www.youtube.com/@dsysd-dev

Follow me on twitter 🐥

Join me on Twitter for a daily dose of knowledge, fascinating trivia, and valuable insights. Let’s embark on a journey of continuous learning and discovery together! Follow me to stay inspired and informed. 🚀

https://twitter.com/dsysd_dev

Subscribe to my Newsletter

If you like my content, then consider subscribing to my free newsletter, to get exclusive, educational, technical, interesting and career related content directly delivered to your inbox

https://dsysd.beehiiv.com/subscribe

Important Links

Thanks for reading the post, be sure to follow the links below for even more awesome content in the future.

Twitter: https://twitter.com/dsysd_dev
Youtube: https://www.youtube.com/@dsysd-dev
Github: https://github.com/dsysd-dev
Medium: https://medium.com/@dsysd-dev
Email: dsysd.mail@gmail.com
Telegram 📚: https://t.me/dsysd_dev_channel
Linkedin: https://www.linkedin.com/in/dsysd-dev/
Newsletter: https://dsysd.beehiiv.com/subscribe
Gumroad: https://dsysd.gumroad.com/
Dev.to: https://dev.to/dsysd_dev/

Top comments (0)