In many languages (including Python & Scala), sets are not much more than syntactic sugar on a hashmap-like datatype.
// Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();
https://github.com/openjdk/jdk/blob/master/src/java.base/sha...A thread-safe implementation is available with `ConcurrentHashMap.newKeySet()`, which uses a similar approach:
public static <K> KeySetView<K,Boolean> newKeySet() {
return new KeySetView<K,Boolean>
(new ConcurrentHashMap<K,Boolean>(), Boolean.TRUE);
}
https://github.com/openjdk/jdk/blob/master/src/java.base/sha...But without generics it must be implemented again for each entry type ...
Kubernetes uses a code generator to automate this, but it's a bit kludge. [3]
[1]: https://pkg.go.dev/k8s.io/apimachinery/pkg/util/sets#Empty
[2]: https://dave.cheney.net/2014/03/25/the-empty-struct
[3]: https://github.com/kubernetes/code-generator/blob/master/cmd...
However sets tend to provide support for… set operations. Which currently can't be done generically in go except through interface{} or specialised, neither of which is a great option.