Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #298 Copy a map

Create the map y by cloning the map x.

y is a shallow copy, not a deep copy.

var y = Map.of(x);
y := make(map[K]V, len(x))
for k, v := range x {
	y[k] = v
}
import "golang.org/x/exp/maps"
y := maps.Clone(x)
my %y = %x;
y = x.copy()
y = x.dup

New implementation...