Go generic set with interesting real world applications

176 views
Skip to first unread message

Andrew Phillips

unread,
Jan 3, 2021, 7:35:38 PM1/3/21
to golang-nuts
I am an avid user of Go who loves how it makes my life so much easier due to its amazing simplicity. I am thrilled to find that a great deal of progress has been made in adding (simple) generic types and functions to the language, which will make my life even simpler!  When I discovered the go2go tool I immediately created a package that implements a variant of the set container. Rather than using a map it uses a slice of ranges.  This has some performance advantages for some types of real-world sets.

Note that it is compatible with Ian Lance Taylor's set package (see src/cmd/go2go/testdata/go2path/src/sets) with the restriction that it only works with integral types. Apart from (possibly huge) memory advantages for some types of sets it also has a few useful characteristics: elements are returned in order + it is possible to invert a set and create a Universal set (inverse of an empty set).

The complete source code can be found at https://github.com/AndrewWPhillips/rangeset. (I wrote it in August but have updated it to the latest syntax.)  I also wrote about it in my blog last November.

Rick

unread,
Jan 4, 2021, 9:56:23 PM1/4/21
to golang-nuts
This is a great practical generics example. Thanks for sharing!
Reply all
Reply to author
Forward
0 new messages