DEV Community

Cover image for Golang 1.21 is here (Part 2)
Pedro Bertao
Pedro Bertao

Posted on • Updated on

Golang 1.21 is here (Part 2)

In this comprehensive post, I will delve into the intricacies of maps and cmp packages in the context of Go programming. I'll shed light on the standout features that have been intelligently incorporated to enhance your day-to-day experience while developing Go applications. Stay tuned for valuable insights and practical tips!


    m := map[string]string{
        "name": "Pedro",
        "age":  "20",

    m2 := map[string]string{
        "name": "Pedro",
        "age":  "20",

    m3 := make(map[string]string)

    r := maps.Equal(m, m2)
    r2 := maps.EqualFunc(m, m2, func(s1, s2 string) bool {
        return m["name"] == m2["name"] && m["age"] == m2["age"]

    fmt.Println("Equal: ", r)
    fmt.Println("Equal Func: ", r2)

    cloned := maps.Clone[map[string]string](m)
    maps.Copy[map[string]string](m3, m)

    fmt.Printf("Cloned: %+v: \n", cloned)
    fmt.Printf("Copied: %+v: \n", m3)
Enter fullscreen mode Exit fullscreen mode

In the presented code snippet, we can observe the introduction of some remarkably useful functions for maps. When using copy it generates a shallow clone of the chosen map, when use clone it generates a full copy of the the map. This becomes incredibly advantageous when the aim is to duplicate a map variable while preserving the integrity of the original data.

The result of the previous code is:

Equal:  true
Equal Func:  true
Cloned: map[age:20 name:Pedro] 
Copied: map[age:20 name:Pedro] 
Enter fullscreen mode Exit fullscreen mode

You can also check the code here


From the documentation, it is simple as:

Package cmp provides types and functions related to comparing ordered values.

It's crucial to grasp that the cmp functions exclusively operate with ordered types constraints, adding a layer of convenience for mathematical operations and more.

Here is the code of two functions available in the cmp package

    var num, num2, num3 int64 = 1, 2, 1
    var str, str2, str3 = "ab", "abc", "ab"

    r := cmp.Compare[int64](num, num2)
    r2 := cmp.Compare[int64](num, num3)

    r3 := cmp.Less[string](str, str2)
    r4 := cmp.Less[string](str, str3)

    fmt.Println("Num is less than Num2: ", r <= 0)
    fmt.Println("Num is equal to Num3: ", r2 >= 0)
    fmt.Println("Num2 is greater than Num: ", r2 >= 0)
    fmt.Println("Str is less than Str2: ", r3)
    fmt.Println("Str is less than Str3 ", r4)
Enter fullscreen mode Exit fullscreen mode

With the result

Num is less than Num2:  true
Num is equal to Num3:  true
Num2 is greater than Num:  true

Str is less than Str2:  true
Str is less than Str3  false
Enter fullscreen mode Exit fullscreen mode

When using cmp.Compare it will return -1,0,1 which means from the documentation:

-1 if x is less than y,
0 if x equals y,
+1 if x is greater than y.

When employing cmp.Less, it evaluates whether the first value is less than or equal to the second. This utility proves highly beneficial for tackling algorithmic challenges.

You can check the code here

And that concludes this post. In our next installment, we'll wrap up with discussions on slices packages. Stay tuned!

Part three

Top comments (0)