Standard library / maps

maps.Clone()

Creates a shallow map copy, preserving nil. Reference-valued entries can still share underlying data. Expected stdout: '1 2\n'.

Formal Syntax & Structure

go syntaxgo
func Clone[M ~map[K]V, K comparable, V any](m M) M

Parameters & Attributes

NameTypeDescription
mMInput map.

Return Value

M

Creates a shallow map copy, preserving nil. Reference-valued entries can still share underlying data.

Working Example

Example Snippetgo
package main
import (  "fmt"  "maps")
func main() {  a := map[string]int{"a": 1}  b := maps.Clone(a)  b["a"] = 2  fmt.Println(a["a"], b["a"])}
Go Reference