Predeclared type

Go comparable type

Predeclared constraint interface for type parameters that support equality. It cannot be used as an ordinary variable type. Since Go 1.20, some types that are not strictly comparable can satisfy it; interface-containing values can still panic on equality. Example output: 'true false'.

Formal Syntax & Structure

go syntaxgo
comparable

Working Example

Example Snippetgo
package main
import (  "fmt")
func equal[T comparable](a, b T) bool { return a == b }func main() {  fmt.Println(equal(4, 4), equal("a", "b"))}
Go Reference