Algorithm


  1. It looks like you're interested in information about the Go programming language's map data structure. In Go, a map is a built-in data type that associates values of one type (the "key") with values of another type. It's somewhat similar to a dictionary or hash table in other programming languages.

    Here's a brief overview of how maps work in Go:

    1. Declaration:

      go
      // Syntax: var mapVariable map[keyType]valueType var myMap map[string]int

      2. Initialization:

      go
      // To create a map, you need to use the make function
      myMap = make(map[string]int)

      3. Adding and Updating Elements:

      go
      myMap["one"] = 1 myMap["two"] = 2 myMap["three"] = 3 // Updating an existing value myMap["two"] = 22

      4. Accessing Elements:

      go
      value := myMap["one"]

      5. Deleting Elements:

      go
      delete(myMap, "two")

      6. Checking if a Key Exists:

      go
      value, exists := myMap["two"] // 'exists' will be true if the key is present, false otherwise

      7. Iterating Over a Map:

      go
      for key, value := range myMap {
          fmt.Println(key, value)
      }

 

Code Examples

#1 Map in Golang Programming

Code - Golang Programming

// Program to create a map and print its keys and values

package main
import "fmt"

func main() {

  // create a map
  subjectMarks := map[string]float32{"Golang": 85, "Java": 80, "Python": 81}

  fmt.Println(subjectMarks)
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
map[Golang:85 Java:80 Python:81]

#2 Access Values of a Map in Golang Programming

Code - Golang Programming

package main
import "fmt"

func main() {

  // create a map
  flowerColor := map[string]string{"Sunflower": "Yellow", "Jasmine": "White", "Hibiscus": "Red"}

  // access value for key Sunflower
  fmt.Println(flowerColor["Sunflower"])  // Yellow

  // access value for key Hibiscus
  fmt.Println(flowerColor["Hibiscus"])  // Red

}
Copy The Code & Try With Live Editor

#3 Change value of a map in Golang Programming

Code - Golang Programming

package main
import "fmt"

func main() {

  // create a map
  capital := map[string]string{ "Nepal": "Kathmandu", "US": "New York"}
  fmt.Println("Initial Map: ", capital)

  // change value of US to Washington DC
  capital["US"] = "Washington DC"

  fmt.Println("Updated Map: ", capital)
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Initial Map: map[Nepal: Kathmandu US: New York]
Updated Map: map[Nepal: Kathmandu US: Washington DC]

#4 Add Element of Golang Map Element

Code - Golang Programming

package main
import "fmt"

func main() {

  // create a map
  students := map[int]string{1: "John", 2: "Lily"}
  fmt.Println("Initial Map: ", students)

  // add element with key 3
  students[3] = "Robin"

  // add element with key 5
  students[5] = "Julie"

  fmt.Println("Updated Map: ", students)
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Initial Map: map[1:John 2:Lily]
Updated Map: map[1:John 2:Lily 3:Robin 5:Julie]

#5 Delete Element of Golang Map Element

Code - Golang Programming

package main
import "fmt"

func main() {

  // create a map
  personAge := map[string]int{"Hermione": 21, "Harry": 20, "John": 25}
  fmt.Println("Initial Map: ", personAge)

  // remove element of map with key John
  delete(personAge, "John")

  fmt.Println("Updated Map: ", personAge)
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Initial Map: map[Harry:20 Hermione:21 John:25]
Updated Map: map[Harry:20 Hermione:21]

#6 Looping through the map in Golang Programming

Code - Golang Programming

package main
import "fmt"

func main() {

  // create a map
  squaredNumber := map[int]int{2: 4, 3: 9, 4: 16, 5: 25}

  // for-range loop to iterate through each key-value of map
  for number, squared := range squaredNumber {
    fmt.Printf("Square of %d is %d\n", number, squared)
  }

}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Square of 2 is 4
Square of 3 is 9
Square of 4 is 16
Square of 5 is 25
Advertisements

Demonstration


Go Lang Programming map