Maps EqualFunc Value Comparison

Compare two maps with maps.EqualFunc and delegate value comparison to slices.Equal, so nested slices match element-wise instead of by identity.

GoData Integrity

Code

gosnippet
26 lines
package main

import (
	"fmt"
	"maps"
	"slices"
)

func main() {
	left := map[string][]int{"a": {1, 2}, "b": {3}}
	right := map[string][]int{"a": {1, 2}, "b": {3}}

	// slice values are not comparable, so EqualFunc takes the comparison
	same := maps.EqualFunc(left, right, func(x, y []int) bool {
		return slices.Equal(x, y)
	})
	fmt.Println("equal values:", same)

	right["b"] = []int{3, 4}
	fmt.Println("after edit:", maps.EqualFunc(left, right, slices.Equal))
	fmt.Println("key count:", len(left), len(right))

	// different key sets short-circuit before the callback runs
	extra := map[string][]int{"a": {1, 2}, "b": {3}, "c": nil}
	fmt.Println("extra key:", maps.EqualFunc(left, extra, slices.Equal))
}

The sequence at runtime

Maps EqualFunc Value Comparison is built around the function main.

Points of care

  • No package manager involved — every call resolves to a built-in API.

Putting it to use

Signature: func main()

The run emits 3 labeled sections — equal values:, after edit:, key count: — one per step.

Core lines

return slices.Equal(x, y)

Interactive Tools for This Topic

FAQ

What does Maps EqualFunc Value Comparison print when run?
No setup needed: run the file and look for labeled lines like `equal values:`, `after edit:`, `key count:` in the output.