We welcome you to subscribe, read, comment and like!!!!

preface

What is the single most important data structure to learn about in Java? I think it’s bound to be a collection. Collections play an important role in Java. So, we are already familiar with the Map structure in Java, but in the Go world, there is still a Map data structure.

Today, we will learn to learn, understand next ah!

What is the Map

Maps are one of the most useful data structures. It can be stored in key-value pairs and does not allow duplicate keys.

A map is a key-value pair storage container. Provides fast and efficient lookups, and does not allow duplicate keys, but duplicate values.

GoLang declares the Map structure

Now, how do we declare a Map structure in Go

package main
 
import (
    "fmt"
)
 
func main(a) {
    var names map[int]string    // name map has int keys and string values
}
Copy the code

In our example, the key is of type Int and the value type is string.

To initialize the Map

Let’s learn how to initialize a Map structure.

Use the Make() function

The make() function can be used to initialize the map as follows:

package main
 
import (
    "fmt"
)
 
func main(a) {
    var names = make(map[int]string)
    names[0] = "John"
    names[1] = "Jane"
 
    fmt.Println(names)  // map[0:John 1:Jane]
}
Copy the code
Use the syntax
package main
 
import (
    "fmt"
)
 
func main(a) {
    var names = map[int]string {
        0: "John".1: "Jane".// last comma is a must
    }
     
     
    fmt.Println(names)  // prints map[0:John 1:Jane]
}
Copy the code

Use maps in GoLang

In Go, you can insert, delete, and find keys in a Map just as you would in Java.

Insert key in the map

Currently, there are two ways to insert keys. Initialize by inserting key or using index syntax.

package main
 
import (
    "fmt"
)
 
func main(a) {
    var names = make(map[int]string)
     
    names[0] = "Freddy"    // indexed insertion
    names[1] = "Shawn"
     
    fmt.Println(names)  // prints map[0:Freddy 1:Shawn]
}
Copy the code

Get the value in the map

We can use index notation to get the value associated with key from the map, as follows:

package main
 
import (
    "fmt"
)
 
func main(a) {
    var names = make(map[int]string)
     
    names[0] = "Freddy"    // indexed insertion
    names[1] = "Shawn"
     
    fmt.Println(names[1])  // prints Shawn
}
Copy the code
Check whether the key exists

When we try to get a value, we get a Boolean value. This helps to check whether the key exists.

package main
 
import (
    "fmt"
)
 
func main(a) {
    var names = make(map[int]string)
     
    names[0] = "Freddy"    // indexed insertion
    names[1] = "Shawn"
     
    fred, exists := names[0]
    if(exists) {
        fmt.Printf("%s exists", fred)  // prints "Freddy exists"}}Copy the code
Delete the Map key

We use the delete function to remove the key from the Map.

package main
 
import (
    "fmt"
)
 
func main(a) {
    var names = make(map[int]string)
     
    names[0] = "Freddy"    // indexed insertion
    names[1] = "Shawn"
    names[2] = "Batman"
     
    // delete shawn
    delete(names, 1) 
    fmt.Println(names)     // prints map[0:Freddy 2:Batman]
}
Copy the code
Traverse Map

With scopes, we can traverse the Map and get both Key and Value

package main
 
import (
    "fmt"
)
 
func main(a) {
    var names = make(map[int]string)
     
    names[0] = "Freddy"    // indexed insertion
    names[1] = "Shawn"
    names[2] = "Batman"
    names[3] = "Spiderman"
    names[4] = "Joker"
     
    for _, name := range names {
        fmt.Println(name)
    }
     
    // prints: 
        // Joker
        // Freddy
        // Shawn
        // Batman
        // Spiderman
}
Copy the code

conclusion

Let’s go from simple to simple and learn about Map operations.

Walking… Learning.