{"id":13411674,"url":"https://github.com/deckarep/golang-set","last_synced_at":"2026-04-21T02:11:01.100Z","repository":{"id":9319207,"uuid":"11162159","full_name":"deckarep/golang-set","owner":"deckarep","description":"A simple, battle-tested and generic set type for the Go language. Trusted by Docker, 1Password, Ethereum and Hashicorp.","archived":false,"fork":false,"pushed_at":"2025-03-14T21:32:36.000Z","size":438,"stargazers_count":4504,"open_issues_count":11,"forks_count":281,"subscribers_count":50,"default_branch":"main","last_synced_at":"2025-05-13T11:03:09.503Z","etag":null,"topics":["concurrency","datastructures","generics","go","golang","set","threadsafe"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/deckarep.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-07-03T21:52:01.000Z","updated_at":"2025-05-13T06:36:00.000Z","dependencies_parsed_at":"2023-01-13T15:17:11.765Z","dependency_job_id":"aebe6a3f-850e-4924-8f1e-1236eb94e57b","html_url":"https://github.com/deckarep/golang-set","commit_stats":{"total_commits":159,"total_committers":39,"mean_commits":4.076923076923077,"dds":0.5660377358490566,"last_synced_commit":"b710ba428a62a0e5686e4be2bb52f221e540d9fb"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deckarep%2Fgolang-set","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deckarep%2Fgolang-set/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deckarep%2Fgolang-set/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deckarep%2Fgolang-set/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/deckarep","download_url":"https://codeload.github.com/deckarep/golang-set/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253929359,"owners_count":21985802,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["concurrency","datastructures","generics","go","golang","set","threadsafe"],"created_at":"2024-07-30T20:01:15.646Z","updated_at":"2026-04-21T02:11:01.094Z","avatar_url":"https://github.com/deckarep.png","language":"Go","readme":"![example workflow](https://github.com/deckarep/golang-set/actions/workflows/ci.yml/badge.svg)\n[![Go Report Card](https://goreportcard.com/badge/github.com/deckarep/golang-set/v2)](https://goreportcard.com/report/github.com/deckarep/golang-set/v2)\n[![GoDoc](https://godoc.org/github.com/deckarep/golang-set/v2?status.svg)](http://godoc.org/github.com/deckarep/golang-set/v2)\n\n# golang-set\n\nThe missing `generic` set collection for the Go language.  Until Go has sets built-in...use this.\n\n## Psst\n* Hi there, 👋! Do you use or have interest in the [Zig programming language](https://ziglang.org/) created by Andrew Kelley? If so, the golang-set project has a new sibling project: [ziglang-set](https://github.com/deckarep/ziglang-set)! Come check it out!\n\n## Update 4/20/2026\n* Packaged version: `2.9.0` contains the following:\n  *  Address an edge case where a deadlock may occur with `Each` should a panic occur: [PR #164](https://github.com/deckarep/golang-set/pull/164)\n  *  BSON Marshaling support for Marshaling and Unmarshaling: [PR #142](https://github.com/deckarep/golang-set/pull/142)\n  *  New PopN method: [PR #166](https://github.com/deckarep/golang-set/pull/166)\n  *  Reduced allocations by using capacity arg for some methods that may allocate: [PR #171](https://github.com/deckarep/golang-set/pull/171)\n\n## Update 3/14/2025\n* Packaged version: `2.8.0` introduces support for true iterators for Go 1.23+. Please see [issue #141](https://github.com/deckarep/golang-set/issues/141)\nfor further details on the implications of how iterations work between older Go versions vs newer Go versions. Additionally, this\nrelease has a minor unit-test spelling fix.\n\n## Update 12/3/2024\n* Packaged version: `2.7.0` fixes a long-standing bug with *JSON Unmarshaling*. A large refactor in the interest of performance\nintroduced this bug and there was no way around it but to revert the code back to how it was previously. The performance\ndifference was likely negligible to begin with. JSON Marshaling and Unmarshaling is now properly supported again without\nneeding to do workarounds.\n\n## Update 3/5/2023\n* Packaged version: `2.2.0` release includes a refactor to minimize pointer indirection, better method documentation standards and a few constructor convenience methods to increase ergonomics when appending items `Append` or creating a new set from an exist `Map`.\n* supports `new generic` syntax\n* Go `1.18.0` or higher\n* Workflow tested on Go `1.20`\n\n![With Generics](new_improved.jpeg)\n\nComing from Python one of the things I miss is the superbly wonderful set collection.  This is my attempt to mimic the primary features of the set collection from Python.\nYou can of course argue that there is no need for a set in Go, otherwise the creators would have added one to the standard library.  To those I say simply ignore this repository and carry-on and to the rest that find this useful please contribute in helping me make it better by contributing with suggestions or PRs.\n\n## Install\n\nUse `go get` to install this package.\n\n```shell\ngo get github.com/deckarep/golang-set/v2\n```\n\n## Features\n\n* *NEW* [Generics](https://go.dev/doc/tutorial/generics) based implementation (requires [Go 1.18](https://go.dev/blog/go1.18beta1) or higher)\n* One common *interface* to both implementations\n  * a **non threadsafe** implementation favoring *performance*\n  * a **threadsafe** implementation favoring *concurrent* use\n* Feature complete set implementation modeled after [Python's set implementation](https://docs.python.org/3/library/stdtypes.html#set).\n* Exhaustive unit-test and benchmark suite\n\n## Trusted by\n\nThis package is trusted by many companies and thousands of open-source packages. Here are just a few sample users of this package.\n\n* Notable projects/companies using this package\n  * Ethereum\n  * Docker\n  * 1Password\n  * Hashicorp\n\n## Star History\n\n[![Star History Chart](https://api.star-history.com/svg?repos=deckarep/golang-set\u0026type=Date)](https://star-history.com/#deckarep/golang-set\u0026Date)\n\n\n## Usage\n\nThe code below demonstrates how a Set collection can better manage data and actually minimize boilerplate and needless loops in code. This package now fully supports *generic* syntax so you are now able to instantiate a collection for any [comparable](https://flaviocopes.com/golang-comparing-values/) type object.\n\nWhat is considered comparable in Go? \n* `Booleans`, `integers`, `strings`, `floats` or basically primitive types.\n* `Pointers`\n* `Arrays`\n* `Structs` if *all of their fields* are also comparable independently\n\nUsing this library is as simple as creating either a threadsafe or non-threadsafe set and providing a `comparable` type for instantiation of the collection.\n\n```go\n// Syntax example, doesn't compile.\nmySet := mapset.NewSet[T]() // where T is some concrete comparable type.\n\n// Therefore this code creates an int set\nmySet := mapset.NewSet[int]()\n\n// Or perhaps you want a string set\nmySet := mapset.NewSet[string]()\n\ntype myStruct struct {\n  name string\n  age uint8\n}\n\n// Alternatively a set of structs\nmySet := mapset.NewSet[myStruct]()\n\n// Lastly a set that can hold anything using the any or empty interface keyword: interface{}. This is effectively removes type safety.\nmySet := mapset.NewSet[any]()\n```\n\n## Comprehensive Example\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  mapset \"github.com/deckarep/golang-set/v2\"\n)\n\nfunc main() {\n  // Create a string-based set of required classes.\n  required := mapset.NewSet[string]()\n  required.Add(\"cooking\")\n  required.Add(\"english\")\n  required.Add(\"math\")\n  required.Add(\"biology\")\n\n  // Create a string-based set of science classes.\n  sciences := mapset.NewSet[string]()\n  sciences.Add(\"biology\")\n  sciences.Add(\"chemistry\")\n  \n  // Create a string-based set of electives.\n  electives := mapset.NewSet[string]()\n  electives.Add(\"welding\")\n  electives.Add(\"music\")\n  electives.Add(\"automotive\")\n\n  // Create a string-based set of bonus programming classes.\n  bonus := mapset.NewSet[string]()\n  bonus.Add(\"beginner go\")\n  bonus.Add(\"python for dummies\")\n}\n```\n\nCreate a set of all unique classes.\nSets will *automatically* deduplicate the same data.\n\n```go\n  all := required\n    .Union(sciences)\n    .Union(electives)\n    .Union(bonus)\n  \n  fmt.Println(all)\n```\n\nOutput:\n```sh\nSet{cooking, english, math, chemistry, welding, biology, music, automotive, beginner go, python for dummies}\n```\n\nIs cooking considered a science class?\n```go\nresult := sciences.Contains(\"cooking\")\nfmt.Println(result)\n```\n\nOutput:\n```false\nfalse\n```\n\nShow me all classes that are not science classes, since I don't enjoy science.\n```go\nnotScience := all.Difference(sciences)\nfmt.Println(notScience)\n```\n\n```sh\nSet{ music, automotive, beginner go, python for dummies, cooking, english, math, welding }\n```\n\nWhich science classes are also required classes?\n```go\nreqScience := sciences.Intersect(required)\n```\n\nOutput:\n```sh\nSet{biology}\n```\n\nHow many bonus classes do you offer?\n```go\nfmt.Println(bonus.Cardinality())\n```\nOutput:\n```sh\n2\n```\n\nThanks for visiting!\n\n-deckarep\n","funding_links":[],"categories":["Go","开源类库","数据结构与算法","Data Structures","Miscellaneous","Generators","Data Structures and Algorithms","数据结构","\u003cspan id=\"数据结构-data-structures\"\u003e数据结构 Data Structures\u003c/span\u003e","语言资源库","Data Integration Frameworks","数据结构`go语言实现的数据结构与算法`","Repositories","Uncategorized"],"sub_categories":["数据结构","集","Advanced Console UIs","Standard CLI","Sets","标准 CLI","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e","go"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeckarep%2Fgolang-set","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdeckarep%2Fgolang-set","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeckarep%2Fgolang-set/lists"}