0Pricing
Go Academy · Lesson

Maps as Sets

Use maps for unique values.

Maps as Sets is a free Go Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Go Has No Set Type

Go does not have a built-in set type. The idiomatic way to model a set of unique values is to use a map where the keys are the set elements.

Map of Bools

One approach: map[string]bool. Presence of a key (and value true) means the element is in the set.

package main

import "fmt"

func main() {
	set := map[string]bool{}
	set["apple"] = true
	set["banana"] = true
	fmt.Println(set)
}

Checking Membership

Use comma-ok or read the bool directly to test membership.

package main

import "fmt"

func main() {
	set := map[string]bool{"apple": true}
	fmt.Println(set["apple"])
	fmt.Println(set["cherry"])
}

Map of Empty Structs

A more memory-efficient set uses map[string]struct{}. An empty struct takes zero bytes, so you store only keys.

package main

import "fmt"

func main() {
	set := map[string]struct{}{}
	set["x"] = struct{}{}
	_, ok := set["x"]
	fmt.Println("x in set:", ok)
}

Why Empty Struct

The value never matters in a set; only the key does. struct{}{} signals you intend to ignore values and uses no extra memory per entry.

Adding Elements

Adding a duplicate key has no effect, so the set naturally enforces uniqueness.

package main

import "fmt"

func main() {
	set := map[string]struct{}{}
	set["a"] = struct{}{}
	set["a"] = struct{}{}
	fmt.Println("size:", len(set))
}

Removing Duplicates

A classic use: deduplicate a slice. Add each element to a set, then read back the unique keys.

package main

import "fmt"

func main() {
	input := []string{"a", "b", "a", "c", "b"}
	set := map[string]struct{}{}
	for _, v := range input {
		set[v] = struct{}{}
	}
	fmt.Println("unique count:", len(set))
}

Building a Unique Slice

Combine the set with a result slice to preserve first-seen order while dropping duplicates.

package main

import "fmt"

func main() {
	input := []int{1, 2, 1, 3, 2}
	seen := map[int]struct{}{}
	result := []int{}
	for _, v := range input {
		if _, ok := seen[v]; !ok {
			seen[v] = struct{}{}
			result = append(result, v)
		}
	}
	fmt.Println(result)
}

Set Intersection

To find elements common to two sets, iterate one and keep those present in the other.

package main

import "fmt"

func main() {
	a := map[int]struct{}{1: {}, 2: {}, 3: {}}
	b := map[int]struct{}{2: {}, 3: {}, 4: {}}
	for k := range a {
		if _, ok := b[k]; ok {
			fmt.Println("common:", k)
		}
	}
}

Removing from a Set

Use delete to take an element out of the set.

package main

import "fmt"

func main() {
	set := map[string]struct{}{"a": {}, "b": {}}
	delete(set, "a")
	_, ok := set["a"]
	fmt.Println("a in set:", ok)
}

Choosing bool vs struct{}

Both work. Use map[T]bool when readability of set[x] as a bool helps. Use map[T]struct{} when you want to signal value-irrelevance and save memory in large sets.

Quick Check

Why is map[string]struct{} often preferred over map[string]bool for sets?

Recap

Maps as sets:

  • Go has no set type; use a map keyed by elements
  • map[T]bool or map[T]struct{}
  • Great for deduplication and membership tests

Frequently asked questions

Is the “Maps as Sets” lesson free?

Yes — the full text of “Maps as Sets” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “Maps as Sets”?

Use maps for unique values. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Maps as Sets” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Map Internals
  2. Checking Existence
  3. Maps as Sets
  4. Iteration and Ordering
← Back to Go Academy