Welcome to Golang Programs, Get the best new programs in your inbox, daily.

Maps

A map is a data structure that provides you with an unordered collection of key/value pairs (maps are also sometimes called associative arrays in Php, hash tables in Java, or dictionaries in Python). Maps are used to look up a value by its associated key. You store values into the map based on a key. The strength of a map is its ability to retrieve data quickly based on the key. A key works like an index, pointing to the value you associate with that key.

A map is implemented using a hash table, which is providing faster lookups on the data element and you can easily retrieve a value by providing the key. maps are unordered collections, and there's no way to predict the order in which the key/value pairs will be returned. Every iteration over a map could return a different order.

Declaring and Initializing Maps

Here is a definition of a map type in Go:

map[Key-type]Value-type [Key-type is the type of key. Value-type is the type of value.]

Here is an example of a map in Go:

var x map[string]int [The map type is represented by the keyword map, followed by the key type in brackets and finally the value type. "x is a map of strings to ints"]

There are several ways you can create and initialize maps in Go. You can use the builtin function make, or you can use a map literal.

package main
import (
"fmt"
)
func main() {
	var country map[int]string	// Declaration of Nil map
	country = make(map[int] string)	// Initialization of map using make function
	country[1]="India"				//Add data to map 
	country[2]="China"
	country[3]="Pakistan"
	country[4]="Germany"
	country[5]="Australia"
	country[6]="Indonesia"
	// Run a loop to fetch map elements using range
	for i, j := range country {
		fmt.Printf("Key: %d Value: %s\n", i, j)
	}
}

Declaring and Initializing Maps using map literal

Retrieving a value from a map testing the value for existence

A map books is declared with string as the type for both key and value, and values are initialized using the map literal.

package main
import (
"fmt"
)
func main() {
		books := map[string]string{
			"Khushwant Singh": "A History of Sikhs",
			"Harisen": "Allahabad Prasasti",
			"Akhil Sharma": "An Obedient Father",
			"Premendra Mitra": "Agamikal",
			"Vikram Seth": "An Equal Music",
		}
		for writers := range books {
      		fmt.Println("Book:",books[writers],"-Writer-",writers)
  		}
  		// Retrieve the value for the key "Harisen".
		value, exists := books["Harisen"]
		// Did this key exist?
		if exists {
			fmt.Println(value)
		}
}

The delete Function

To remove items from a map, use the built-in function delete by providing the key. The delete function removes an element for the given key from the map and doesn't return anything.

Here is the code block that deletes an element from books map for the key "Harisen" .

package main
import (
"fmt"
)
func main() {
		// Create map
		books := map[string]string{
			"Khushwant Singh": "A History of Sikhs",
			"Harisen": "Allahabad Prasasti",
			"Akhil Sharma": "An Obedient Father",
			"Premendra Mitra": "Agamikal",
			"Vikram Seth": "An Equal Music",
		}
		// Print map
		for writers := range books {
      		fmt.Println("Book:",books[writers],"-Writer-",writers)
  		}
		// Delete an item from map
 		delete(books,"Harisen");
		// Print map
  		for writers := range books {
      		fmt.Println("Book:",books[writers],"-Writer-",writers)
  		}
}