Convert a Slice to a Map in Go (Set, Index Map, Group Duplicates)

Build a map from a slice in Go: use elements as keys for set-like maps, pick struct fields as keys, map indices to values, or map keys to slices of positions for duplicates.

Published

Updated

Read time 2 min read

Reviewed byDeepak Prasad

Convert a Slice to a Map in Go (Set, Index Map, Group Duplicates)

You often golang convert slice into map to get O(1) lookups: treat slice entries as set keys, index structs by an ID, or group duplicates into map[K][]V. Maps need comparable keys; slices and maps cannot be keys themselves. For map basics, see maps in Go.

Tested with Go 1.24 on Linux.


Elements as keys (set-like)

go
package main

import "fmt"

func main() {
	numbers := []int{1, 2, 3, 4}
	m := make(map[int]bool)
	for _, n := range numbers {
		m[n] = true
	}
	fmt.Println(len(m), m[3])
}
Output

You should see 4 and true.


Slice of structs keyed by a field

go
package main

import "fmt"

type Item struct {
	ID   int
	Name string
}

func main() {
	items := []Item{{1, "a"}, {2, "b"}}
	byID := make(map[int]Item)
	for _, it := range items {
		byID[it.ID] = it
	}
	fmt.Println(byID[2].Name)
}
Output

You should see b.


Index as key

go
package main

import "fmt"

func main() {
	numbers := []int{10, 20, 30}
	m := make(map[int]int)
	for i, v := range numbers {
		m[i] = v
	}
	fmt.Println(m[1])
}
Output

You should see 20.


Duplicates: map key to slice of indices

go
package main

import "fmt"

func main() {
	numbers := []int{1, 2, 2, 3}
	m := make(map[int][]int)
	for i, n := range numbers {
		m[n] = append(m[n], i)
	}
	fmt.Println(m[2])
}
Output

You should see [1 2] (indices where value 2 appears).


Summary

Converting a slice to a map is a for range over the slice plus clear rules for keys, values, and overwrite behavior. Use map[K]bool or struct{} for sets, map[K]T keyed by IDs for structs, and map[K][]int (or similar) when duplicates must be preserved.


References


Frequently Asked Questions

1. How do I convert a slice to a map in Go?

Loop with for range; choose each element or an index as the map key and assign the value you need. Maps require comparable keys.

2. What happens if slice elements repeat when used as map keys?

Later iterations overwrite earlier ones unless you use a map of slices or merge logic. Only one value can exist per key.

3. Can I use a struct field as the map key from a slice of structs?

Yes, if the field type is comparable—for example use an ID field as the key and store the whole struct as the value.

4. How is this related to general maps?

See maps in Go for syntax and iteration; slice-to-map is just population logic on top of make(map[K]V).

5. Where can I read about struct-to-map patterns?

Related material appears in posts about structs and maps when projecting rows into lookup tables.
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive …