複製地圖

與切片一樣,貼圖儲存基礎資料結構的引用。因此,通過將其值分配給另一個變數,只傳遞引用。要複製地圖,必須建立另一個地圖並複製每個值:

// Create the original map
originalMap := make(map[string]int)
originalMap["one"] = 1
originalMap["two"] = 2

// Create the target map
targetMap := make(map[string]int)

// Copy from the original map to the target map
for key, value := range originalMap {
  targetMap[key] = value
}