A map is an unordered and changeable collection of key-value pairs. The map data structure is used for fast lookups, retrieval, and deletion of data based on keys. It is one of the most picked data structures in computer science.

Golang_Maps_1

It maps keys to values. The keys are unique identifiers within a map, while the values may not be. A map key can have the data type for which the equality operator (==) is defined. The data types are:

  • Arrays
  • Booleans
  • Numbers
  • Strings
  • Pointers
  • Structs

Types such as Slices, Maps and Functions are invalid as equality operator (==) is not defined for them. Go provides a built-in map type with a default value of nil. This article will educate you how to use Golang’s built-in map type.

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

How to Create Maps in Golang?

  • Using the Map Literal:

Map with key-value pair

Syntax:

map[Key_Type]Value_Type{key1: value1, ..., keyN: valueN}

Golang_Maps_2.

Golang_Maps_3

  • Using Make Function

A map can also be created by passing the type of key and value to the make() function. It is an inbuilt function. With the following syntax, we can create a new map.

Syntax:

make(map[type of key]type of value) 

Golang_Maps_4

Golang_Maps_5.

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

Creating an Empty Map

We can create an empty map by following two ways. One is by using the make() function and the other by using the map literal.

The zero or default value of a map is nil. A run-time panic will occur if you try to add elements to a nil map. Hence the map has to be initialized before adding elements. As we already know, the make() function always returns an initialized map. So we can add value to it.

Golang_Maps_6

Golang_Maps_7

How to Iterate Over a Map?

Using the range for loop, we can iterate over a map. The values printed through the loop may vary because the map is an unordered collection.

Golang_Maps_8.

Golang_Maps_9

Accessing Map Elements

We can access the map elements with the following syntax.

Syntax:

value = map_name[key]

Golang_Maps_10
Golang_Maps_11

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

Updating and Adding Map Elements

We can update and add the map elements with the following syntax.

Syntax:

map_name[key] = value

Golang_Maps_12.

 

Remove Element from Map

We can remove the map elements using the delete() function.

Syntax:

delete(map_name, key)

Golang_Maps_14
Golang_Maps_15.

Free Course: Programming Fundamentals

Learn the Basics of ProgrammingEnroll Now
Free Course: Programming Fundamentals

Check for Specific Elements in a Map

We can check if a certain key exists in a map with the following syntax.

Syntax:

val, ok :=map_name[key]

We can use the blank identifier (_) in place of val, to check the existence of a certain key.

Golang_Maps_16
Golang_Maps_17

Maps Are Reference Types

Maps are reference types. When you assign a map to a new variable, both refer to the same underlying data structure. The changes done by one variable will affect the content of the other.

Golang_Maps_18

Golang_Maps_19.

Length of Map

We can find the length of a map using the len() function. The len() function calculates the number of elements in a map. 

Golang_Maps_20

Golang_Maps_21

Full Stack Java Developer Course

In Partnership with HIRIST and HackerEarthEXPLORE COURSE
Full Stack Java Developer Course

Maps Equality

Maps can be compared by checking each one's individual elements one by one. It can't be compared using the == operator. The == operator only works to check if a map is nil.

Golang_Maps_22
Golang_Maps_23

Master front-end and back-end technologies and advanced aspects in our Post Graduate Program in Full Stack Web Development. Unleash your career as an expert full stack developer. Get in touch with us NOW!

Conclusion

In this tutorial, we learned how to create and initialize and declare maps, how to create an Empty Map, how to iterate over a Map, how to access map elements, how to update and add map elements, remove elements from map and a lot more operations of Map. This article is comprehensive, covering all the key topics relating to Map in Golang. 

If you aim to build a software development career, you can check the Post-Graduate Program in Full Stack Development by Simplilearn. It can be the ideal solution to help you develop your career in the right direction.

In case you have queries or would like to provide your input to our editorial team regarding this “The Supreme Guide to Golang Maps” tutorial, feel free to use the comments section below. Our team of SMEs will get back to you soon!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.
  • *According to Simplilearn survey conducted and subject to terms & conditions with Ernst & Young LLP (EY) as Process Advisors