Golang maps.Values
last modified May 1, 2026
This tutorial explains how to use the maps.Values function in Go.
We'll cover map operations with practical examples of retrieving and iterating over values using Go's modern iterator pattern.
The maps.Values function returns an iterator (specifically an iter.Seq[V]) over the values in a map.
Introduced in Go 1.23, this function allows for memory-efficient iteration.
However, it is important to note that for a simple loop, ranging over the map directly is more idiomatic and efficient.
maps.Values shines when you need to pass values to other functions or compose them with other iterators.
Performance Tip: If you only need to loop through values, usefor _, v := range myMap. Usemaps.Valueswhen you need to treat the values as a standalone sequence, for example, when usingslices.Collect.
Basic maps.Values Example
The simplest use of maps.Values involves retrieving all values from a map.
Because it returns an iterator, we use slices.Collect to convert those values into a printable slice.
package main
import (
"fmt"
"maps"
"slices"
)
func main() {
grades := map[string]int{
"Alice": 85,
"Bob": 90,
"Charlie": 78,
}
// Convert the iterator to a slice for printing
values := slices.Collect(maps.Values(grades))
fmt.Println("All grades:", values)
}
We create a map with student names as keys and their grades as values.
maps.Values yields the grades one by one. slices.Collect gathers them into a slice.
The output might be [85 90 78], though the order is randomized.
Summing Map Values
You can iterate directly over the result of maps.Values.
While your linter might suggest ranging over the map directly, this example shows how iterators integrate with standard loops.
package main
import (
"fmt"
"maps"
)
func main() {
prices := map[string]int{
"apple": 50,
"banana": 30,
"orange": 40,
}
total := 0
// Ranging over the iterator
for v := range maps.Values(prices) {
total += v
}
fmt.Println("Total price:", total)
}
The iterator yields each price, and we add it to the total.
The output will be Total price: 120.
Working with Struct Values
maps.Values works with maps containing complex types.
This example extracts Employee structs and prints their specific fields.
package main
import (
"fmt"
"maps"
)
type Employee struct {
Name string
Salary int
}
func main() {
employees := map[string]Employee{
"e1": {"Alice", 50000},
"e2": {"Bob", 60000},
"e3": {"Charlie", 55000},
}
fmt.Println("All employees:")
for emp := range maps.Values(employees) {
fmt.Printf("%s earns %d\n", emp.Name, emp.Salary)
}
}
The iterator yields each Employee struct.
Inside the loop, we access the Name and Salary fields directly.
Filtering Values
Filtering is a common task when processing map data. Here, we use the values iterator to find scores that meet a certain threshold.
package main
import (
"fmt"
"maps"
)
func main() {
scores := map[string]int{
"Alice": 95,
"Bob": 85,
"Charlie": 92,
"Dave": 88,
}
highScores := []int{}
for score := range maps.Values(scores) {
if score >= 90 {
highScores = append(highScores, score)
}
}
fmt.Println("High scores (90+):", highScores)
}
We iterate over the scores and append only those 90 or higher to our slice.
The output will be High scores (90+): [95 92] (in no specific order).
Empty Map Behavior
maps.Values safely handles empty maps by returning an empty iterator.
package main
import (
"fmt"
"maps"
"slices"
)
func main() {
emptyMap := map[string]int{}
values := slices.Collect(maps.Values(emptyMap))
fmt.Println("Values from empty map:", values)
fmt.Println("Length of values:", len(values))
}
Since the map is empty, the iterator finishes immediately.
slices.Collect returns an empty slice [] with a length of 0.
Performance Considerations
While iterators are efficient for large datasets because they don't require pre-allocating a large slice, there is a small overhead compared to a direct map range.
package main
import (
"fmt"
"maps"
"slices"
"time"
)
func main() {
largeMap := make(map[string]int, 1_000_000)
for i := 0; i < 1_000_000; i++ {
largeMap[fmt.Sprintf("key%d", i)] = i
}
start := time.Now()
// Collecting 1 million values into a slice
values := slices.Collect(maps.Values(largeMap))
fmt.Println("Time to collect values into slice:", time.Since(start))
fmt.Println("Number of values collected:", len(values))
}
This example measures how long it takes to pull every value from a massive map into a single slice.
The conversion is efficient, but remember that slices.Collect does allocate memory for the resulting slice.
Practical Example: Data Analysis
In data analysis, we often need to perform multiple operations on values. Here, we collect values to a slice to easily calculate both the sum and the average.
package main
import (
"fmt"
"maps"
"slices"
)
func main() {
sales := map[string]float64{
"Jan": 15000.50,
"Feb": 18000.75,
"Mar": 16500.25,
"Apr": 17000.00,
}
// Collecting values to get access to len() and indexing
values := slices.Collect(maps.Values(sales))
sum := 0.0
for _, v := range values {
sum += v
}
var average float64
if len(values) > 0 {
average = sum / float64(len(values))
}
fmt.Printf("Total Sales: %.2f\n", sum)
fmt.Printf("Average Monthly Sales: %.2f\n", average)
}
We extract the sales figures into a slice. This allows us to use len(values) to calculate the average.
The output will show the total and the average, formatted to two decimal places.
Source
This tutorial covered the maps.Values function in Go.
We learned how to use iterators to extract values and when to use slices.Collect for further processing.
Author
List all Go tutorials.