Understanding the maps Package in Go v1.21
The versatile and efficient Go programming language has won over developers worldwide, thanks to its simplicity and robust capabilities. With each new release, Go continues to evolve, surprising the community with exciting new features. In the latest Go version, v1.21, there’s a hidden treasure for those who appreciate working with maps — the “maps” package.
Picture a world where manipulating maps becomes effortless, and handling data feels like pure magic. In this article, we’ll guide you on a journey to unlock the captivating potential of the “maps” package in Go v1.21. Get ready to witness how this package elevates your data manipulation experience and boosts your productivity.
This article will equip you with the knowledge of the “maps” package’s spells, enabling you to harness its power effortlessly. We’ll delve into the secret incantations of each function and illustrate their abilities with captivating code examples.
1. Clone()
The Clone()
function creates a shallow copy of a map. The new map will have the same keys and values as the original map.
package main
import (
"fmt"
"maps"
)
func main() {
originalMap := map[string]int{"a": 1, "b": 2, "c": 3}
//Cloning the map
clonedMap := maps.Clone(originalMap)
//Store the address of the map
originalAddress := &originalMap
clonedAddress := &clonedMap
fmt.Printf("Original Map - %v | Address - %p\n", originalMap, originalAddress)
fmt.Printf("Cloned Map - %v | Address - %p\n", clonedMap, clonedAddress)
}
//Output
Original Map - map[a:1 b:2 c:3] | Address - 0x1400004c020
Cloned Map - map[a:1 b:2 c:3] | Address - 0x1400004c028
Note that the values are same but the addresses are different from the cloned one, so changing the cloned values won’t affect the original one.
2. Copy()
The Copy()
function copies all key-value pairs from the source map source
and adds them to the destination map destination
. When a key in source
already exists in destination
, the corresponding value in destination
will be overwritten with the value from source
.
package main
import (
"fmt"
"maps"
)
func main() {
destinationMap := map[string]int{"a": 1, "b": 2}
sourceMap := map[string]int{"b": 3, "c": 4}
// Copying source map into destination map
maps.Copy(destinationMap, sourceMap)
fmt.Println("Destination Map:", destinationMap)
}
//Output
Destination Map: map[a:1 b:3 c:4]
3. DeleteFunc()
The DeleteFunc()
function allows deleting key-value pairs from a map m
based on a provided function. The function should accept a key and a value as parameters and return a boolean indicating whether the key-value pair should be deleted.
package main
import (
"fmt"
"maps"
)
func main() {
originalMap := map[string]int{"apple": 1, "banana": 2, "cherry": 3, "date": 4}
// Delete key-value pairs where the value is even
maps.DeleteFunc(originalMap, func(key string, value int) bool {
return key == "apple"
})
fmt.Println("Modified Map:", originalMap)
}
//Output
Modified Map: map[banana:2 cherry:3 date:4]
4. Equal()
The Equal
function compares two maps, m1
and m2
, to check if they contain the same key-value pairs irrespective of the order. Values are compared using the ==
operator.
package main
import (
"fmt"
"maps"
)
func main() {
map1 := map[string]int{"a": 1, "b": 2}
map2 := map[string]int{"b": 2, "a": 1}
// Check if map1 and map2 are equal
isEqual := maps.Equal(map1, map2)
fmt.Println("Are they equal?", isEqual)
}
//Output
Are they equal? true
5. EqualFunc()
The EqualFunc
function is similar to Equal
but allows developers to provide a custom comparison function to compare the values in the maps. Keys are compared using the ==
operator.
package main
import (
"fmt"
"maps"
)
func main() {
map1 := map[string]int{"a": 1, "b": 2}
map2 := map[string]int{"a": 1, "b": 4}
// Check if map1 and map2 are equal based on custom value comparison function
isEqual := maps.EqualFunc(map1, map2, func(v1, v2 int) bool {
return v1%2 == v2%2 // Compare if both values are even or odd
})
fmt.Println("Are maps equal?", isEqual)
}
//Output
Are maps equal? true
Note that maps package is supported only in go 1.21+. That’s the end of this blog.
If you like this post, please do like and add your comments and follow my page for more go related blogs.
- https://medium.com/@quicktechlearn/go-an-evolving-language-in-this-modern-era-b5d4e58dd838
- https://medium.com/@quicktechlearn/how-slices-affect-the-performance-of-your-code-in-go-how-can-they-be-mitigated-9e6f3d90e2e6
- https://medium.com/@quicktechlearn/how-to-sync-goroutines-in-go-how-to-use-wait-groups-7fef6741950