Golang Program that Uses Structs As Map Keys

Learn via video courses
Topics Covered

Overview

Golang is an effective programming language with numerous features for dealing with structured data. One of these capabilities is utilizing structs as keys in maps. In this article, we will look at creating a Golang program that uses structs as map keys.

What is a Struct?

A struct in Golang is a composite data type aggregating zero or more values of different types. More complicated data types that can represent various items are made using structures.

The keywords type and struct are used to define a struct, followed by a list of fields wrapped in braces. Here is an example of a Golang struct definition.

In this example, we have defined a struct called Person, which has Name and Age properties. The Age column has the type integer, while the Name field has the type string.

Using Structs as Map Keys

Maps are used in Golang to link values to keys. The map keyword is used to define a map, followed by the types of the key and value, both of which are surrounded in brackets. Here is a sample of a Golang map definition.

In this example, a map called myMap has been defined with string keys and integer values.

We must ensure that a struct is similar to utilize it as a key in a map. If all of a struct's fields are comparable, the struct is comparable. The fields must, therefore, be of a primitive type, a struct type having comparable fields, or a pointer to a similar type.

Example

Here is an example of a Golang program that uses a struct as a key in a map:

Output:

Explanation:

In this example, we have defined a struct called Person, which has the properties Name and Age. Then, Person keys and integer values are used to construct a map called personMap.

John and Jane are created as two instances of the Person struct, and their corresponding Name and Age fields are set. Then, using John and Jane as keys and 100 and 200 as values, we use the make() function to build an empty personMap map and assign values to it.

The personMap map's contents are then printed to the console.

Iterating over a Map

Iterating over a map: You can also run a loop to access and operate each map key individually.

Output:

Explanation:

This Go program demonstrates the usage of structs as keys in a map. It defines a struct called Address with fields for a person's name, city, and pin code. The program creates instances of this struct, uses them as keys in a map called sample, and associates each address with an integer value. It then iterates through the map, printing both the address and its associated value. Additionally, it showcases how to access a specific field (in this case, Name) of each struct key when iterating through the map.

Adding Key

Adding "key: value" pairs in the map is done using the given syntax:

If a key-value pair already exists in the map it will just update the old pair with the new.

Output:

Explanation:

This Go program defines a Student struct with three fields: Name, RollNo, and Course. It then creates a map mp with Student keys and integer values. Initially, it populates the map with two key-value pairs. It then adds a third key-value pair to the map by creating a new Student instance with the same values as an existing one. Additionally, it increments the value associated with a fourth key (tinaKey) in the map. Finally, it prints the original and updated maps, demonstrating how keys can be added and manipulated in a Go map using struct keys.

Deleting a Struct Key from the Map

Using the delete() function, you can remove a struct key from the map. It is a built-in function with no return value and does nothing if the key is deleted from the provided map. The following is the syntax for the same: delete(map_name, struct_key)

Output:

The remaining map after deletion:

Explanation:

This Go program defines a Student struct with three fields: Name, RollNo, and Course. It creates four instances of the Student struct with different values. A map mp is created with Student keys and integer values.

The program then deletes a specific key-value pair (a4) from the map using the delete function. It prints the remaining elements in the map after deletion, iterating through the map and displaying the Name, RollNo, and the associated integer value.

In summary, the code demonstrates the creation and manipulation of a map with Student struct keys, showcasing how to delete a key-value pair and display the remaining entries in the map.

Checking the Existence of a "key: value" Pair

You can determine whether or not a struct is included in the map. The syntax to check whether a "struct_key" pair exists in the map is provided below:

Output:

Explanation:

This Go program defines a Student struct with three fields: Name, RollNo, and Course. It then creates four instances of the Student struct, populating them with student information.

Next, it creates a map called mp where the keys are Student structs and the values are strings representing student roles. It initializes this map with the four student instances.

The program performs lookups in the map using specific Student instances as keys. It checks if a key is present in the map and prints a boolean result along with the associated value. This demonstrates how to check for the presence of a struct key in a map and retrieve its associated value.

Finally, it prints the results for two key lookups (a4 and a2) to show whether the keys exist in the map and their associated values.

Conclusion

  1. One of the strong features of the Golang programming language is the use of structs as map keys.
  2. Structs can be used as keys to build more intricate data structures that make storing and retrieving values easier.
  3. You can fully benefit from the adaptability and strength of Golang maps by making sure your structures are comparable.

I hope you liked this article.