Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang
go
map
key
value

What is a Golang map?

Educative Answers Team

Maps (also called hashes or dicts in other languages) are one of Go’s built-in associative data types. A map maps keys to values. In that sense, a map stores 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 widely-used data structures in computer science.

Syntax

A map is declared using the following syntax:

var m map[KeyType]ValueType
  • m: the name of the map variable.
  • KeyType (Optional): the data type of the keys in the map. This data type can also be declared at the time of initialization.
  • ValueType: the data type of the value in the key-value pairs in a map.

Map types are reference types like pointers or slices; so, the value of the above m is nil as it doesn’t point to an initialized map. Any attempt to write to a nil map will cause a runtime panic.

var m = make(map[KeyType]ValueType)

Code

The following line of code declares a map with String keys that map to Integer values.

var m map[string]int

To initialize a map, the following built-in make function is required.

m = make(map[string]int)
package main
import "fmt"

func main() {
	// declaring a map
	var mymap map[string]int
	// initializing the map
	mymap = make(map[string]int)

	if mymap == nil {
		fmt.Println("mymap is nil")
	} else {
		fmt.Println("mymap is not nil")
	}
}

Since each key can map to, at most, one value, a map cannot contain identical keys.

Inserting values in the map

Values can be inserted in the map using the following syntax:

mymap["Key"] = Value

This can be further understood using the code:

package main
import "fmt"

func main() {
	// declaring a map
	var mymap map[string]int
	// initializing the map
	mymap = make(map[string]int)

	mymap["Pakistan"] = 92
  	mymap["India"] = 91

  fmt.Println(mymap)
}

Accessing values in the map

Individual values in a map can be accessed using the following syntax:

val = mymap["Key"]

This can be further understood using the code:

package main
import "fmt"

func main() {
	// declaring a map
	var mymap map[string]int
	// initializing the map
	mymap = make(map[string]int)

	mymap["Pakistan"] = 92
  	mymap["India"] = 91
	
	// Accessing a specific value in the map
	var stored_value = mymap["Pakistan"]
  	fmt.Println(stored_value)
}

Deleting values from a map

To delete values from a map, use the following syntax:

delete(map, key)
  • map is the name of the map variable.
  • key is the value of the key-value pair we want to delete from the map.

This can be further understood using the code:

package main
import "fmt"

func main() {
	// declaring a map
	var mymap map[string]int
	// initializing the map
	mymap = make(map[string]int)

	mymap["Pakistan"] = 92
  	mymap["India"] = 91
	
  	fmt.Println("Before deletion, mymap:", mymap)
	delete(mymap, "Pakistan")
  	fmt.Println("After deletion, mymap:", mymap)
}

Iterating over a map

We can also iterate over a map using the range form of for-loop. The range form gives you the key and value pair in every iteration:

package main
import "fmt"

func main() {
	// declaring a map
	var mymap map[string]int
	// initializing the map
	mymap = make(map[string]int)

	mymap["Pakistan"] = 92
  	mymap["India"] = 91
	mymap["USA"] = 1

	for country, code := range mymap {
		fmt.Println(country, code)
	}

}

A map is an unordered collection; therefore, the iteration order of a map is not guaranteed to be the same every time you iterate over it.


RELATED TAGS

golang
go
map
key
value
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring