deckarep / golang-set

A simple, battle-tested and generic set type for the Go language. Trusted by Docker, 1Password, Ethereum and Hashicorp.
Other
4.15k stars 274 forks source link

Document thread-safe set usage #123

Closed efulmo closed 1 year ago

efulmo commented 1 year ago

The README mentions there are 2 implementations that share the same interface. It's unclear though:

  1. How to use these implementations?
  2. Which one do I get when I do NewSet[T]()?
  3. Why is mapset used as a default module name in samples? set seems much more straightforward.
deckarep commented 1 year ago

Unless you specifically ask the API for a “thread-safe” set using the appropriate constructor methods you will get a Set type back that is not thread safe. In other words it does not use any locking internally and this means you don’t have to worry about taking an unnecessary performance hit with respect to synchronization.

It’s up to you and your application design to use the appropriate constructor method. If you don’t need synchronization just use the NewSet related methods and if you do, use the NewThreadSafe related constructor methods.

There are only two choices and knowing which to use is only something you can answer.