Welcome To Golang By Example

Allowed key and value types for a map in Go (Golang)

Maps are golang builtin datatype similar to the hash table which maps a key to a value. . Below is the format for a map:

map[key_type]value_type

Both key_type and value_type can be of different type or same type. For below example the key type is string and value type is int

map[string]int

Allowed Key types in a Map

The map key can be any type that is comparable. Some of the comparable types as defined by go specification are

Some of the types which are not comparable as per go specification and which cannot be used as a key in a map are.

Reference – https://golang.org/ref/spec#Comparison_operators

Allowed Value types in a Map

Value can be of any type in a map.