Avatar ·

Why is there no built-in "Set" data type in Go and how to implement it?

Hello everyone! I'm a beginner Go developer who switched from Python and encountered an unexpected problem.\n\nIn Python, I constantly used `set()` to check element uniqueness, remove duplicates from a list, and quickly check membership. For example:\n\n```python\nmy_list = [1, 2, 2, 3, 3, 4]\nunique = set(my_list) # {1, 2, 3, 4}\nif 3 in unique: # O(1) check\n print("Found!")\n```\n\nWhen I started writing in Go, I expected to find something similar in the standard library, but to my surprise, there is neither a `Set` type nor generics (before version 1.18). I had to write workarounds:\n\n```go\n// Attempt 1: use map\nseen := make(map[int]bool)\nitems := []int{1, 2, 2, 3, 3, 4}\nfor _, v := range items {\n seen[v] = true\n}\n// But this is inconvenient - need to check ok, verbose\n