Maps in Golang

  • date 26th August, 2022 |
  • by Prwatech |
  • 0 Comments

How to make and use maps in Golang

Maps in Go (or Golang) are a fundamental data structure used for storing key-value pairs. A map allows efficient lookup, insertion, deletion, and update operations based on keys. In Go, a map is declared using the map keyword followed by the key and value types enclosed in square brackets (map[keyType]valueType). Maps in Go are unorder collections, meaning that the iteration order of elements is not guarante. The keys within a map must be unique, and each key can map to exactly one value. To interact with maps in Go, you can use built-in functions like make to create a new map instance, len to get the number of key-value pairs, and the delete function to remove a key-value pair from the map. Accessing a value associated with a key can be done using square brackets (map[key] syntax), which returns the value if the key exists or a zero-value if the key is not present. Go maps are widely use in various applications for efficient data retrieval and management, such as caching, data indexing, and maintaining associative relationships between entities. Understanding how to create, manipulate, and utilize maps is essential for effective Go programming and building scalable applications.

Using Simple example we will learn mapping in Go lang :

 
  1. Go program to illustrate how to create maps
    package main
    import "fmt"
    
    func main() {
    var map_1 map[int]int   // Creating and initializing empty map
    
    if map_1 == nil      // Checking if the map is nil or not
    
            fmt.Println("True")
        } else {
            fmt.Println("False")
        }
    
      	// Creating and initializing a map
        map_2: = map[int]string {
            90: "Math",
            91: "Statistic",
            92: "Python",
            93: "SQL",
            94: "Machine Learning ",
        }
        fmt.Println("Map-2: ", map_2)
    }
    
    Output :
    PS C:\GO_Language\Maps> go run maps.go
    True
    Map-2:  map[90:Math 91:Statistic 92:Python 93:SQL 94:Tablue]
    
  2. Go program to illustrate, iterate a map using the range for loop. The value of this loop may vary because the map is an unordered collection
    package main
    import "fmt"
    func main() {
        M_a_p: = map[int]string {
            90: "Math",
            91: "Statistic",
            92: "Python",
            93: "SQL",
            94: " Machine Learning ",
        }
        for id, pet := range m_a_p {
            fmt.Println(id, pet)
        }
    }
    
    Output :
    PS C:\GO_Language\Maps> go run maps1.go
    92 Python
    93 SQL
    94  Machine Learning 
    90 Math
    91 Statistic
    

        

       Using the Mark Function:

    1. Go program to illustrate the difference between declaring an empty map using with the make()function and without it
      package main
      
      import "fmt"
      
      func main() {
          var My_course = make(map[float64]string)
          fmt.Println(My_map)
      
          My_course[1.3] = "Data Scientist"
          My_course[1.5] = "Data Analyst"
          fmt.Println(My_course)
      }
      
      Output :
      PS C:\GO_Language\Maps> go run maps2.go
      map[]
      map[1.3:Data Scientist 1.5:Data Analyst]
      
    2. Go program to illustrate the difference between declaring an empty map using with the make()function and without it.
      package main
      
      import "fmt"
      
      // Main function
      func main() {
      
          // Creating and initializing a map
          m_a_p := map[int]string{
      
              90: "Math",
              91: "Statistic",
              92: "Python",
              93: "SQL",
              94: "Tablue",
          }
      
          // Iterating map using for rang loop
          for id, pet := range m_a_p {
      
              fmt.Println(id, pet)
          }
      }
      
      Output :
      PS C:\GO_Language\Maps> go run maps3.go
      90 Math
      91 Statistic
      92 Python
      93 SQL
      94 Tablue
      
       

Quick Support

image image