I'm excited for generics to land in Golang so we can have efficient set implementations instead of abusing maps.
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...
// 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...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.