Created
August 31, 2023 09:36
-
-
Save pedrobertao/5d11c8ff0f3de6f8878640620ea9f439 to your computer and use it in GitHub Desktop.
maps package in golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"golang.org/x/exp/maps" | |
) | |
func main() { | |
m := map[string]string{ | |
"name": "Pedro", | |
"age": "20", | |
} | |
m2 := map[string]string{ | |
"name": "Pedro", | |
"age": "20", | |
} | |
m3 := make(map[string]string) | |
// Equal and Equal Func | |
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 and Copy | |
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment