create a map with a key of type bool in go

In Go, we can create a map with a key of type bool using the following syntax:

main.go
mapName := make(map[bool]valueType)
36 chars
2 lines

Here, valueType denotes the type of the values that will be stored in the map.

Example:

main.go
boolMap := make(map[bool]string)
boolMap[true] = "John"
boolMap[false] = "Jane"
fmt.Println(boolMap[true]) // Output: John
fmt.Println(boolMap[false]) // Output: Jane
167 chars
6 lines

In the above example, we have created a map with a bool key and string value. We are adding two key-value pairs to the map and then printing them using their respective keys. Here, true and false are the two keys of the map and John and Jane are the corresponding values.

related categories

gistlibby LogSnag