Go

Fix Go "panic: runtime error: comparing uncomparable type []int" — Slice, Map and Function Values as Map Keys and in == Comparisons

The compiler stops this mistake wherever it can see it — and it cannot see inside an interface. Below, each shape gets its own section: the direct forms that never build, the interface forms that build, run, and then panic at the comparison or the map access, the one comparison that is always legal, and the replacements that compare contents or use a key type a map can actually hash.

Written November 21, 2026 · Tested with Go 1.26.4 · 23 min read

By the end of this guide
  • The legal shapes first: comparable values, working keys
  • Written directly, a slice comparison never builds
  • The panic itself: two interface values holding slices
  • A struct with a slice field: the panic names the struct

What this guide is

You hit panic: runtime error: comparing uncomparable type []int when two values are compared with == and at least one of them turns out, at run time, to hold a slice.

The rule underneath is short. The equality operators == and != apply to operands of comparable types. Slice, map, and function types are not comparable. As a special case, a slice, map, or function value may be compared to the predeclared identifier nil, and that is the only comparison those types get. Arrays are comparable if their element types are comparable, and structs are comparable if all their field types are comparable, so comparability is a property of the whole value, not just its outer type.

If that is the rule, why is the failure a panic instead of a compile error? Because most of the time it is a compile error, the next section but one shows the compiler refusing a direct slice comparison outright. The panic exists for the one place the compiler cannot look: inside an interface value. Interface types that are not type parameters are comparable, and two interface values are equal if they have identical stored types and equal stored values. Comparing them means comparing whatever is actually inside, and if that stored type is not comparable, the comparison itself panics at run time. The specification says so in plain terms: a comparison of two interface values with identical stored types causes a run-time panic if that type is not comparable, and the same applies when comparing arrays of interface values or structs with interface-valued fields.

That is why this panic survives testing. Nobody writes a == b on two slices, the compiler will not let them. They write it on two any values, two values returned from a generic helper, two entries decoded into an interface, and for months everything inside happens to be strings and ints. Then one caller stores a slice, and the comparison that always worked detonates on a value it was never checked against.

The map-key shapes in this guide are the same rule seen from the other side. A map's key type must have == and != fully defined for its operands, so the key type must not be a function, map, or slice. Written directly, that is a compile error too. Through an interface key type it builds, and then panics with a different message, hash of unhashable type, because hashing the key is where the runtime meets the value. Two messages, one rule: the compiler stops every shape it can see, and the runtime stops the rest at the first use.

All examples use Go 1.26.4. Each one is a single main.go in one module. Make the module once, from an empty directory:

Terminal
$ mkdir uncomparable
$ cd uncomparable
$ go mod init example.com/uncomparable
go: creating new go.mod: module example.com/uncomparable

After that, swap in each program as the new main.go. Transcripts come from go run -trimpath main.go. The -trimpath flag shortens file paths in the trace, you see ./main.go:10 instead of a long absolute path. Line numbers match either way. Tested with Go 1.26.4.

Before the failures, the baseline. Arrays compare element by element, structs compare field by field, and any comparable type can be a map key, including an array, which is the fact the fixes at the end lean on.

Go
// main.go
package main

import "fmt"

func main() {
    a := [3]int{1, 2, 3}
    b := [3]int{1, 2, 3}
    fmt.Println(a == b)
    type Point struct{ X, Y int }
    fmt.Println(Point{1, 2} == Point{1, 2})
    m := map[[3]int]string{}
    m[a] = "array key"
    m[[3]int{4, 5, 6}] = "other"
    fmt.Println(m[a], len(m))
}
Terminal
$ go run -trimpath main.go
true
true
array key 2

Three facts to keep. [3]int{1, 2, 3} == [3]int{1, 2, 3} is true: arrays hold a fixed number of elements, so comparing all of them is well defined. A struct of two int fields compares true the same way, field by field. And an array works as a map key, two distinct array values with equal contents find the same entry, which is why the map reports length 2 after two different keys were stored and reads the first one back.

Everything that fails below fails by one step away from this baseline: replace the array with a slice, put a slice inside the struct, or hide any of it inside an interface, and the same-looking code stops building or starts panicking.

Written directly, a slice comparison never builds

Start with the program most people imagine produced the panic in the title. It did not, it cannot run at all:

Go
// main.go
package main

import "fmt"

func main() {
    a := []int{1, 2}
    b := []int{1, 2}
    fmt.Println(a == b)
}
Terminal
$ go run -trimpath main.go
# command-line-arguments
./main.go:9:14: invalid operation: a == b (slice can only be compared to nil)

That is a compile error: invalid operation: a == b (slice can only be compared to nil). The compiler knows both operands are []int, knows slices are not comparable, and refuses. Writing the slices as literals changes only the wording of the same refusal, the message names the literal form instead of the variables, not the outcome. There is no executable, no stack trace, and nothing to debug at run time.

Hold on to this, because it decides where to look when the panic does appear. If a comparison in your code can be seen by the compiler, it is already safe or already rejected. A comparing uncomparable type panic therefore never comes from a comparison written on the uncomparable type itself. It comes from a comparison written on something the compiler treats as comparable, an interface, whose contents turned out not to be. Search for the panic starting from your any values, not from your slice variables.

The panic itself: two interface values holding slices

Put the same two slices inside interface values and the identical comparison builds, runs, and panics:

Go
// main.go
package main

import "fmt"

func main() {
    var a any = []int{1, 2}
    var b any = []int{1, 2}
    fmt.Println("before")
    fmt.Println(a == b)
}
Terminal
$ go run -trimpath main.go
before
panic: runtime error: comparing uncomparable type []int

goroutine 1 [running]:
main.main()
    ./main.go:10 +0xd0
exit status 2

before prints. Then a == b panics with the exact message in this guide's title, naming the stored type inside the interfaces: []int. The comparison of the two interface values is legal, interfaces are comparable, so the program builds. Equality of interfaces means identical stored types and equal stored values, and checking the second half requires comparing two []int values, which has no definition. The runtime refuses at the comparison, in the goroutine that ran it, and the program ends.

The type named in the message is whatever was inside. Two any values holding map[string]int values panic with panic: runtime error: comparing uncomparable type map[string]int, and two holding function values panic with panic: runtime error: comparing uncomparable type func(), same line shape, same mechanism, different stored type in the message. When you meet this panic in a log, the type it names is the first real clue: it tells you exactly what kind of value a caller stored where your code expected something it could compare.

One more detail in the specification is worth knowing before you go hunting: the panic requires the two stored types to be identical. An interface holding a slice compared against an interface holding a string is simply unequal, the types differ, the comparison returns false, and no value comparison is ever attempted. The crash needs two values of the same uncomparable type meeting in one comparison, which is exactly what happens when a collection, a cache, or a deduplication pass compares values that all came from the same producer.

A struct with a slice field: the panic names the struct

Comparability is recursive, and the struct shape shows it. A struct is comparable only if every field type is comparable, so a struct with a []int field is not comparable, written directly, comparing two of them is a compile error with the message invalid operation: a == b (struct containing []int cannot be compared). Through interfaces, it is a panic, and the message names the struct, not the field:

Go
// main.go
package main

import "fmt"

type Record struct {
    Name string
    Tags []int
}

func main() {
    var a any = Record{Name: "a", Tags: []int{1}}
    var b any = Record{Name: "a", Tags: []int{1}}
    fmt.Println("before")
    fmt.Println(a == b)
}
Terminal
$ go run -trimpath main.go
before
panic: runtime error: comparing uncomparable type main.Record

goroutine 1 [running]:
main.main()
    ./main.go:15 +0x145
exit status 2

Read the message literally: comparing uncomparable type main.Record. Nothing in it mentions Tags or []int. In a large codebase that is a real diagnostic cost, the panic tells you which struct type reached the comparison, and you then have to find which of its fields made it uncomparable: a slice field, a map field, a function field, or another struct that itself contains one, at any depth. The same recursion runs through arrays: an array whose element type is uncomparable is uncomparable, and per the specification, comparing arrays of interface values or structs with interface-valued fields can panic the same way when the values inside turn out uncomparable, even though the outer type looked fine to the compiler.

The practical lesson is about type design at boundaries. A struct that will be stored in interfaces and compared, deduplicated, or used anywhere equality is assumed needs comparable fields all the way down, or it needs to stay out of interface-typed comparisons entirely and be compared by a function written for it, like the fixed programs below.

As a map key type, a slice never builds either

The map version of the direct mistake is stopped just as early. A map key type must have equality fully defined, so a slice cannot be named as one:

Go
// main.go
package main

import "fmt"

func main() {
    m := map[[]int]string{}
    fmt.Println(m)
}
Terminal
$ go run -trimpath main.go
# command-line-arguments
./main.go:7:11: invalid map key type []int

invalid map key type []int, a compile error, before any map exists. Map and function key types are refused the same way. This is the compiler applying the key rule from the specification directly: the comparison operators must be fully defined for operands of the key type, thus the key type must not be a function, map, or slice.

So a map in a running program never has a declared slice key type. What it can have is an interface key type, map[any]string is perfectly legal, because interface keys are allowed on the condition, stated in the specification, that the comparison operators are defined for the stored key values, and failure causes a run-time panic. That conditional sentence is the whole next section.

Through an interface key, the map panics, with a different message

Declare the map with an interface key type, then use a slice as a key. The program builds, and the insertion itself panics, but not with the message in this guide's title:

Go
// main.go
package main

import "fmt"

func main() {
    m := make(map[any]string)
    fmt.Println("before")
    m[[]int{1, 2}] = "x"
    fmt.Println(m)
}
Terminal
$ go run -trimpath main.go
before
panic: runtime error: hash of unhashable type []int

goroutine 1 [running]:
main.main()
    ./main.go:9 +0xac
exit status 2

panic: runtime error: hash of unhashable type []int. To insert a key, the runtime has to hash it, and a slice has no hash, so the failure surfaces at the hash, one step before any comparison of keys could happen. Expecting the word comparing in this message sends a log search in the wrong direction; for map shapes, grep for hash of unhashable type.

A lookup fails too, and it does not matter that the key being looked up was never inserted, the lookup has to hash its key before it can discover that. The exact wording depends on whether the map already holds entries. Looking up a map[any]string that holds entries, with a key whose stored value is a map[string]int, panics with panic: runtime error: hash of unhashable type map[string]int, and with a function value as the key it panics with panic: runtime error: hash of unhashable type func(). The same lookup on an empty or nil map prints the colon form instead: panic: hash of unhashable type: map[string]int, and likewise panic: hash of unhashable type: func() or panic: hash of unhashable type: []int for the other key types. Insert and lookup, slice, map, and function keys: one message family, naming the stored type of the offending key each time, but grep for both spellings, with and without runtime error: and the colon.

This shape has a specific natural habitat: a map[any]... used as a general-purpose index, cache, or memo table, fed by values of whatever type callers happen to pass. Every comparable key works, so the table earns trust, until the first caller keys it by a slice. If a map's keys arrive as interfaces from outside your code, the key's stored type is an input, and like every input it needs a decision in front of it: reject uncomparable stored types before the map access, or stop keying by raw values and key by something derived and comparable, as the last fixed program does.

The one comparison that always works: against nil

There is exactly one comparison a slice, map, or function value can take part in, and it is the special case the specification carves out, comparison to nil:

Go
// main.go
package main

import "fmt"

func main() {
    var s []int
    var m map[string]int
    var f func()
    fmt.Println(s == nil)
    fmt.Println(m == nil)
    fmt.Println(f == nil)
    fmt.Println(s != nil)
}
Terminal
$ go run -trimpath main.go
true
true
true
false

All four lines print, no panic, exit clean. A nil slice, a nil map, and a nil function each compare true against nil, and the != form works as its mirror. This is not a loophole in the rule; it is the defined test for whether such a value was ever initialized, and it is the comparison behind the everyday if s == nil and if m == nil checks. What it cannot do is tell you anything about contents: a non-nil empty slice and a slice with elements are both simply not nil. The moment the question is about what is inside the value rather than whether it exists, you need a contents comparison, which is what the fixes provide.

The fixed programs: compare contents, or change the key

Two different needs hide behind this panic, and they have different fixes. Sometimes the code wanted to know whether two values have the same contents. Sometimes it wanted to use a value as a map key. Here is the contents fix first:

Go
// main.go
package main

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

func main() {
    a := []int{1, 2, 3}
    b := []int{1, 2, 3}
    fmt.Println(slices.Equal(a, b))
    fmt.Println(slices.Equal(a, []int{1, 2}))
    x := map[string]int{"a": 1}
    y := map[string]int{"a": 1}
    fmt.Println(maps.Equal(x, y))
    fmt.Println(reflect.DeepEqual(a, b))
    fmt.Println(reflect.DeepEqual(x, y))
    var n []int
    fmt.Println(slices.Equal(n, []int{}))
    fmt.Println(reflect.DeepEqual(n, []int{}))
}
Terminal
$ go run -trimpath main.go
true
false
true
true
true
true
false

Work down the output. slices.Equal reports whether two slices are equal, same length, all elements equal, so the equal pair prints true and the pair with different lengths prints false. maps.Equal reports whether two maps contain the same key/value pairs, with values compared using ==. reflect.DeepEqual answers true for both the slices and the maps: it is the general deep comparison, defined across arrays, structs, maps, slices, and the rest, where values of distinct types are never deeply equal.

The last two lines are the caveat that decides between them, printed rather than asserted. slices.Equal treats a nil slice and an empty slice as equal, its documentation says empty and nil slices are considered equal, so it prints true. DeepEqual keeps them apart: slice values are deeply equal only when both are nil or both are non-nil, with the same length and deeply equal elements, so a nil slice and []int{} print false. Neither answer is wrong; they answer different questions. If nil-versus-empty matters to your code, it often does at API boundaries, where nil can mean absent, that distinction, not convenience, picks the function. DeepEqual has one more boundary worth knowing from its documentation: function values are deeply equal only if both are nil, and otherwise not deeply equal at all, so there is no contents comparison for functions, in this package or any other. Two functions can be tested against nil, and that is the end of what can be asked about their equality.

Now the key fix. The value itself does not have to change, the key does. Derive a comparable key from the value, or switch to a type that was comparable all along:

Go
// main.go
package main

import "fmt"

func main() {
    byArray := map[[3]int]string{}
    byArray[[3]int{1, 2, 3}] = "slice contents as array"
    fmt.Println(byArray[[3]int{1, 2, 3}])
    byString := map[string]string{}
    key := fmt.Sprint([]int{1, 2, 3})
    byString[key] = "slice contents as string"
    fmt.Println(byString["[1 2 3]"])
    byID := map[int]string{}
    byID[42] = "record 42"
    fmt.Println(byID[42])
    type Point struct{ X, Y int }
    byPoint := map[Point]string{}
    byPoint[Point{1, 2}] = "point key"
    fmt.Println(byPoint[Point{1, 2}])
}
Terminal
$ go run -trimpath main.go
slice contents as array
slice contents as string
record 42
point key

Four working keys, in the order to consider them. If the length is fixed, an array is the same contents in a comparable type, the baseline program at the top already showed array keys working, and converting a fixed-length slice to an array keeps the key as the contents themselves. If the length varies, a string form of the contents is comparable and hashable; here fmt.Sprint renders the slice as [1 2 3], and that string is the key, a workable derivation when the rendering is unambiguous for your element type, and one to replace with an explicit encoding when it is not. If the value has an identity separate from its contents, most records do, key by that ID and let two records with equal contents stay distinct, which is usually what a cache or index actually meant. And if the key is naturally a small bundle of comparable fields, a struct of those fields is a comparable key directly, no derivation step at all.

Notice what none of these fixes does: none of them recovers from the panic, and none of them compares interfaces more carefully. The programs are safe because the uncomparable value never reaches a comparison or a hash, the comparison is done by a function built for contents, or the key was comparable before the map ever saw it.

The fixes, ranked

Compare contents with slices.Equal or maps.Equal when that is the question. These are the direct replacements for the == you could not write: same length and equal elements for slices, same key/value pairs for maps. Reach for them before reflect.DeepEqual; they say exactly which comparison is happening, and slices.Equal's treatment of nil and empty as equal is the common intent.

Use reflect.DeepEqual when the value is nested or mixed, and decide the nil question first. Deep equality handles structs containing slices, slices of maps, and the other shapes the flat helpers do not. Its price is its distinctions: nil and empty are different, values of distinct types are never equal, and function values are equal only when both are nil. If those distinctions match your meaning, it is the right tool; if they do not, write the comparison field by field instead of accepting a near miss.

Change the key, not the map. A value that needs to be a key needs a comparable form: an array for fixed length, a string derivation for variable contents, an ID for identity, or a struct of comparable fields. Pick the form that matches what the key means, contents, rendering, or identity, because the map will treat any two equal keys as the same entry, whether you meant that or not.

Keep uncomparable values out of interface-typed comparisons and interface-keyed maps. Where any values flow into == or into a map[any]..., the stored type is an input. Check it, switch on it, or restructure so the comparison happens on concrete types the compiler can vet. Every shape in this guide that panicked entered through an interface; every shape the compiler stopped was written on concrete types.

Do not answer the panic with recover, and do not force comparability by converting through a string you have not checked. Recovering hides which caller stored the uncomparable value, and the next comparison panics again. A string derivation whose rendering can collide, two different values printing the same, silently merges distinct keys. Both moves trade a loud failure for a quiet wrong answer.

Which fix for which cause

  • comparing uncomparable type []int at an == on interface values Cause: Two any values hold slices and were compared. Fix: Compare the concrete values with slices.Equal, or keep slices out of the interface comparison
  • comparing uncomparable type map[string]int or func() Cause: Interface values hold a map or a function and were compared. Fix: Test functions and maps against nil only; compare map contents with maps.Equal or reflect.DeepEqual
  • comparing uncomparable type main.Record (your struct's name) Cause: A struct with an uncomparable field, at any depth, reached an interface comparison. Fix: Make the fields comparable all the way down, or compare the struct with a purpose-written function or reflect.DeepEqual
  • hash of unhashable type []int on a map access Cause: A slice was used as a key in a map with an interface key type. Fix: Key by an array, a string derivation, or an ID instead; validate stored key types before the access
  • hash of unhashable type (two spellings, see above) on a lookup with a map or function key Cause: A map or function value was used as a lookup key through an interface. Fix: Same as above, the lookup hashes its key before it can miss; the message spelling depends on whether the map holds entries
  • Build fails with slice can only be compared to nil or invalid map key type []int Cause: The mistake is written on concrete types. Fix: The compiler has found it for you, apply the contents comparison or the comparable key at that line; there is no run-time issue to chase

Checklist

  • Find every == and != whose operands are interface values. List what stored types can actually arrive there. If a slice, map, or function is on that list, the comparison is a panic waiting for its first matching pair.
  • Find every map with an interface key type. Trace what callers use as keys. A single uncomparable stored key panics on insert and on lookup alike.
  • Audit structs that flow into interfaces for uncomparable fields at any depth, slice, map, and function fields, and nested structs that contain them. The panic names the outer struct, not the field, so this audit is faster done by reading the type than by reading logs.
  • Replace contents questions with slices.Equal, maps.Equal, or reflect.DeepEqual, and write down which nil-versus-empty answer your code needs before choosing.
  • Replace value keys with comparable keys: array, string derivation, ID, or a struct of comparable fields, chosen by what the key means, contents or identity.
  • Keep the nil comparisons. s == nil, m == nil, and f == nil are legal, idiomatic, and often the only equality question those types can answer.
  • Test the interface boundaries with the uncomparable values, not just the comparable ones: a slice in the any, a slice key in the map. The comparable cases pass with or without your fix; the uncomparable cases are the ones that decide.

Sources

  • The Go Programming Language, The Go Programming Language Specification, for comparison operators (the equality operators == and != apply to operands of comparable types; struct types are comparable if all their field types are comparable; array types are comparable if their array element types are comparable; interface types that are not type parameters are comparable, and two interface values are equal if they have identical stored types and equal stored values or if both have value nil; a comparison of two interface values with identical stored types causes a run-time panic if that type is not comparable, including when comparing arrays of interface values or structs with interface-valued fields; slice, map, and function types are not comparable, but as a special case a slice, map, or function value may be compared to the predeclared identifier nil), and for map types (the comparison operators == and != must be fully defined for operands of the key type, thus the key type must not be a function, map, or slice; if the key type is an interface type, these comparison operators must be defined for the stored key values, and failure will cause a run-time panic).
  • The Go Programming Language, Package slices, for slices.Equal (reports whether two slices are equal: the same length and all elements equal; if the lengths are different, Equal returns false; empty and nil slices are considered equal).
  • The Go Programming Language, Package maps, for maps.Equal (reports whether two maps contain the same key/value pairs, with values compared using ==).
  • The Go Programming Language, Package reflect, for reflect.DeepEqual (values of distinct types are never deeply equal; slice values are deeply equal when both are nil or both are non-nil, they have the same length, and their corresponding elements are deeply equal, so a non-nil empty slice and a nil slice are not deeply equal; func values are deeply equal if both are nil, otherwise they are not deeply equal).

Keep Exploring