golang tutorial - Maps In Golang | Maps Go - golang - go programming language - google go - go language - go program - google language



Maps In Golang

  • Go provides another important data type map which maps unique keys to values.
  • A key is an object that you use to retrieve a value at a later date.
  • Given a key and a value, you can store the value in a Map object. After value is stored, you can retrieve it by using its key.

Defining a map

/* declare a variable, by default map will be nil*/
var map_variable map[key_data_type]value_data_type

/* define the map as nil map can not be assigned any value*/
map_variable = make(map[key_data_type]value_data_type)
click below button to copy the code. By - golang tutorial - team

Example for creation and usage of map

package main

import "fmt"

func main() {
   var currencyAmountMap map[string]string
   /* create a map*/
   currencyAmountMap = make(map[string]string)
   
   /* insert key-value pairs in the map*/
   currencyAmountMap["United States"] = "Dollar"
   currencyAmountMap["Italy"] = "European euro"
   currencyAmountMap["Japan"] = "Japanese yen"
   currencyAmountMap["India"] = "Rupee"
   
   /* print map using keys*/
   for currency := range currencyAmountMap {
      fmt.Println("Amount of",currency,"is",currencyAmountMap[currency])
   }
   
   /* test if entry is present in the map or not*/
   Amount, ok := currencyAmountMap["United Kingdom"]
   /* if ok is true, entry is present otherwise entry is absent*/
   if(ok){
      fmt.Println("Amount of United Kingdom is", Amount)  
   }else {
      fmt.Println("Amount of United Kingdom is not present") 
   }
}
click below button to copy the code. By - golang tutorial - team
golang , gopro , google go , golang tutorial , google language , go language , go programming language

Output for the above golang programming

Amount of India is Rupee
Amount of United States is Dollar
Amount of Italy is European euro
Amount of Japan is Japanese yen
Amount of United Kingdom is not present
golang , gopro , google go , golang tutorial , google language , go language , go programming language

delete() function

  • delete() function is used to delete an entry from the map. It requires map and corresponding key which is to be deleted.
package main

import "fmt"

func main() {   
   /* create a map*/
   currencyAmountMap := map[string] string {"United States":"US Dollar","Italy":"European euro","Japan":"Yen","India":"Rupee"}
   
   fmt.Println("Original map")   
   
   /* print map */
   for currency := range currencyAmountMap {
      fmt.Println("Amount of",currency,"is",currencyAmountMap[currency])
   }
   
   /* delete an entry */
   delete(currencyAmountMap,"United States");
   fmt.Println("Entry for United States is deleted")  
   
   fmt.Println("Updated map")   
   
   /* print map */
   for currency := range currencyAmountMap {
      fmt.Println("Amount of",currency,"is",currencyAmountMap[currency])
   }
}
click below button to copy the code. By - golang tutorial - team
golang , gopro , google go , golang tutorial , google language , go language , go programming language

Output of the above golang program

Original Map
Amount of United States is US Dollar
Amount of Italy is European euro
Amount of Japan is Yen
Amount of India is Rupee
Entry for United States is deleted
Updated Map
Amount of India is Rupee
Amount of Italy is European euro
Amount of Japan is Yen

Sample program for golang maps programming

package main

	import "fmt"

	func main() {
// To create an empty map, use the builtin make:make(map[key-type]val-type).
	    m := make(map[string]int)
// Set key/value pairs using typical name[key] = val syntax.
	    m["k1"] = 7
    m["k2"] = 13
// Printing a map with e.g. Println will show all of its key/value pairs.
	    fmt.Println("map:", m)

// Get a value for a key with name[key].
	    v1 := m["k1"]
    fmt.Println("v1: ", v1)
// The builtin len returns the number of key/value pairs when called on a map.
	    fmt.Println("length:", len(m))

/// The builtin delete removes key/value pairs from a map.
	    delete(m, "k2")
    fmt.Println("map:", m)

// The optional second return value when getting a value from a map indicates if the key was present in the map. This can be used to disambiguate between missing keys and keys with zero values like 0 or "". Here we didn’t need the value itself, so we ignored it with the blank identifier _.
	    _, prs := m["k2"]
    fmt.Println("prs:", prs)
// You can also declare and initialize a new map in the same line with this syntax.
	    n := map[string]int{"Level": 1, "NextLevel": 2}
    fmt.Println("map:", n)
}
click below button to copy the code. By - golang tutorial - team

output

// Note that maps appear in the form map[k:v k:v] when printed with fmt.Println.
	$ go run maps.go 
map: map[k1:7 k2:13]
v1:  7
length: 2
map: map[k1:7]
prs: false
map: map[Level:1 NextLevel:2]

Related Searches to Maps In Golang | Maps Go