Why don't create the type set

It seems that we usually use data struct of set,over make a map of value with a empty struct.
I think it’s very similer,and easy to use,but when use it often,it can be very fussy.
because when init it, we may do like this




and when i want an union or intersection,there’s no build-in function.

also we can write code like blow,

type Set[T comparable] map[T]struct{}

func union[T comparable](s1 Set[T], s2 Set[T]) Set[T] {
	ret := Set[T]{}
	for k, _ := range s1 {
		ret[k] = struct{}{}
	for k, _ := range s2 {
		ret[k] = struct{}{}
	return ret

but i think it can be more elegant if we can use like that


Because set is a collection of unordered unique elements and Golang already has slices as a base type for such collections. Their state (unique or not) depends on the use case. There are third-party libraries which gives you desired functionality or you can always write something yourself. Sadly, I don’t think that it will be added into the language as a standalone type. Since the philosophy is to give basics to make something more complex.

thank you for your replay,i really think it should added to standlone type,because most language treated it as a inner type.and developer often use it.
though, it can take golang take simple for beginner, but most developer may use set frequently.
like in earlier version that, we didn’t have max or min, but we can use it in current version. :smile:

There is a huge discussion over the years for this proposal here. Maybe one day it will lead to something, but that’s the go we live with today. Right now we have to implement such functionality on basic types ourselves or use already written packages, depending on the situation. I came from python and sometimes I really miss sets as well. Everything is coming from the proposals and people’s choices. We have Min and Max now, due to generics, which minimised the required codebase for the implementation of those basic functions. And tbh set could be added as well because of the generics and the fact that you won’t need to write a lot of code.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.