Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,939 questions

51,876 answers

573 users

How to clone a two-dimensional array in Go

1 Answer

0 votes
package main

import "fmt"

func clone2DArray(arr2D [][]int) [][]int {
    // Create a new 2D array with the same dimensions as the original
    cloned := make([][]int, len(arr2D))
    
    for i := range arr2D {
        cloned[i] = make([]int, len(arr2D[i]))
        copy(cloned[i], arr2D[i])
    }
    
    return cloned
}

func main() {
    arr2D := [][]int{
        {1, 2, 3, 0},
        {4, 5, 6, 77},
        {7, 8, 9, 31},
    }

    // Clone the 2D array
    cloned := clone2DArray(arr2D)

    fmt.Println("Original array:", arr2D)
    fmt.Println("Cloned array:", cloned)
}


/*
run:

Original array: [[1 2 3 0] [4 5 6 77] [7 8 9 31]]
Cloned array: [[1 2 3 0] [4 5 6 77] [7 8 9 31]]

*/

 



answered Mar 8, 2025 by avibootz

Related questions

1 answer 95 views
1 answer 126 views
2 answers 216 views
216 views asked Aug 27, 2020 by avibootz
1 answer 174 views
2 answers 242 views
242 views asked Aug 27, 2020 by avibootz
1 answer 141 views
...