Deep dive into Go 1.21 maps package

Nahuel Costamagna

Nahuel Costamagna

· 4 min read
Go 1.21 maps package - Nahuel Costamagna

Introduction

In the realm of Go programming, version 1.21 introduces a fresh perspective on the Maps Package. As a cornerstone of data manipulation, maps enable efficient key-value storage and retrieval. In this article, we'll delve into the enhanced features of Go 1.21's Maps Package, empowering you to harness its potential for streamlined coding and data management. Join us as we journey through the realm of key-value mastery with Go 1.21's Maps Package.

We will discuss the 'maps' package, which allows us to perform various operations on maps in Go.

The first step is to import the maps package (and we'll also import the fmt package to display the examples).

import (
    "fmt"
    "maps"
)

We're going to create a map that we'll use throughout the rest of the post.

myMap := map[string]int{
    "one":   1,
    "two":   2,
    "three": 3,
    "four":  4,
}

and we are going to see those methods next:

  • Clone: we can clone a map
  • Equal: we can compare 2 maps
  • DeleteFunc: we can delete elements of the map
  • Copy: we can copy all map elements to other map

As a clarification, there were 2 methods (Values and Keys) that were removed when the stable version 1.21 was released. Those methods were in beta. (commit here)

Clone

We can clone a map, creating another map:

myOtherMap := maps.Clone(myMap)

fmt.Printf("First Map %v \nClone %v\n", myMap, myOtherMap)
Output:
First Map map[four:4 one:1 three:3 two:2] 
Clone map[four:4 one:1 three:3 two:2]

Equal

We can compare 2 maps. If both maps are equal, the method will return true; otherwise, it will return false.

resutl := maps.Equal(map1, map2)

Example:

r := maps.Equal(myMap, myOtherMap)

fmt.Printf("myMap:      %v \nmyOtherMap: %v\n", myMap, myOtherMap)
fmt.Printf("Compare %t\n", r )

myMap["one"] = 11
r = maps.Equal(myMap, myOtherMap)

fmt.Printf("\n\nmyMap:      %v \nmyOtherMap: %v\n", myMap, myOtherMap)
fmt.Printf("Compare %t\n", r )
Output:
myMap:      map[four:4 one:1 three:3 two:2] 
myOtherMap: map[four:4 one:1 three:3 two:2]
Compare true

myMap:      map[four:4 one:11 three:3 two:2] 
myOtherMap: map[four:4 one:1  three:3 two:2]
Compare false

DeleteFunc

We can delete one or many map elements. In the case of deletion, we can only use the 'Function' functionality.

maps.DeleteFunc(myMap, func(key string, value int) bool {
    ...
})

When it returns true, the key/value pairs will be deleted.

Example:

We delete all elements that have the value 'one'.

fmt.Printf("Original Map: %v\n", myMap)

maps.DeleteFunc(myMap, func(k string, v int) bool {
    return k == "one"
})

fmt.Printf("with Deleted value %v\n", myMap)
Output:
Original Map: map[four:4 one:1 three:3 two:2]
with Deleted value map[four:4 three:3 two:2]

Copy

we can capy all key/values pairs in src adding them to dst.

When a key in src is already present in dst, the value in dst will be overwritten by the associeted with the key in src.

maps.Copy(dst, src)

Example:

dst := map[string]int{
    "four":4, 
    "ten":10,
    "three":3,
    "two":2,
}

src := map[string]int {
    "four":44, 
    "nine":9,
    "one":1,
    "three":3,
    "two":2,
}

fmt.Printf("dst: %v \nsrc: %v\n", dst, src)

maps.Copy(dst, src)
 
fmt.Printf("\ncopied variable (dst): %v\n", dst)
Output:
dst: map[four:4 ten:10 three:3 two:2] 
src: map[four:44 nine:9 one:1 three:3 two:2]

copied variable (dst): map[four:44 nine:9 one:1 ten:10 three:3 two:2]

Conclusion

To sum up, the Go maps package is a versatile and efficient tool for managing key-value pairs. By mastering this package, we gain a fundamental skill to handle various programming tasks effectively. Let's harness the power of maps to elevate our Go programming endeavors, for more information you can see the Go official site here

Nahuel Costamagna

Nahuel Costamagna

FullStack Developer & DevOps