Maps (also called dictionaries) are a very useful tool in helping to store and organize objects to be accessed in an efficient method. Most basic implementations of a map involve using a key to access a value in the map, resulting in key-value pairs, in which one key is associated with a specific value in the map. Within Go, maps follow this definition. This blog will cover the basic use cases of maps in Go, and how a newcomer to the language may utilize them for their applications.

Initialization

Initialization of a map can be done using the make command. This is similar to the initialization of a slice:

mapObject := make(map[string]string)

In this case, mapObject is a map that uses strings as a key to map to another string. When creating a map, the key type must be a type that is Comparable, or more specifically types that can be compared using the == operator. Examples of valid key types include booleans, numbers, strings and several other primitives can be used as keys. For reference on types, check out this link. One extra thing to note is that structs can be used as a key, provided that all the properties of the struct are Comparable.

#go #maps #golang #golang maps

Working with Golang Maps
4.10 GEEK