About Golang Arrays and Handling

Asked 2 years ago, Updated 2 years ago, 64 views

I would like to ask you a question about how to handle the arrays used in Golang.

After the sample code, I will ask you more questions.

package main

import "fmt"

funcmain(){

    x: = [ ] int {1,2,3}
    y —=x

    varz[]int
    for_,v: = range x {
        z = append(z,v)
    }

    x[1] = 4
    fmt.Println(x)
    fmt.Println(y)
    fmt.Println(z)
}

In the code, first create an array called x.You are copying the array to two arrays (y,z) in a different way.
Copy y exactly as it is, and z copies each element of the array one by one.

After that, I just changed the contents of x and printed all the arrays.

I have a question,
Why does the y array change at the same time?
Also, is there a way to prevent an array created like y from synchronizing with x?

Professor, please.

go

2022-09-30 15:44

2 Answers

The question is for a one-dimensional array, but if the copy is for a multidimensional array, you may not be able to use the [...] notation (make the compiler calculate the number of elements in the array).

Consider, for example, the following two-dimensional array:

x:=[...][2]int {{1,2}, {3,4}, {5,6}}
y —=x

In this case, all elements will be copied.

However, if you define the array size of each element as [...][...]int in the same way, you will get a compilation error:

 x: = [...][...] int {{1,2}, {3,4,5}, {6,7,8,9}}

use of [...] array outside of array literal

So if you say [...][]int, it will be defined as an array of slices, so it will be copied as a slice (show copy).

x:=[...][]int {{1,2}, {3,4,5}, {6,7,8,9}}
y —=x
x[1][0]=4
=>
x —[[12][445][6789]]
y: [[12][445][6789]]

You can also specify the maximum size of the element, but

x:=[...][4]int {{1,2}, {3,4,5}, {6,7,8,9}}
y —=x
x[1][0]=4
=>
x —[[1 2000][4 450][6789]]
y:[[1 200][3 4 50][67 89]]

It will be

To copy (deep copy) such a multidimensional array/slice:

Use the deepcopy package

mohae/deepcopy

x:=[][]int {{1,2}, {3,4,5}, {6,7,8,9}}
    y: = deepcopy.If(x).([][]int)
    x[1][0]=4
    =>
    x —[[12][445][6789]]
    y: [[12][345][6789]]
  • Copy by element in loop operation
x:=[][]int {{1,2}, {3,4,5}, {6,7,8,9}}
    y:=make([][]int,len(x))
    for i,v: = range x {
      y[i]=make([]int,len(v))
      copy(y[i],v)
    }
    x[1][0]=4
    =>
    x —[[12][445][6789]]
    y: [[12][345][6789]]


2022-09-30 15:44

[]int{1,2,3} is called a slice literal and creates a slice that references an array instead of an array. If you substitute y, the array is not copied, and y and x refer to the same array.

 x: = [ ] int {1,2,3} // Create an array and replace the slice
y: = x // Copy slice
                     // Type x,y is [ ] int (slice)

If you want to create an array directly, use the array literal [...]int{1,2,3} or [3]int{1,2,3}.

 x: = [...] int {1,2,3} // Create an array
y —Copy the = x// array
                     // The type x,y is [3]int (array)

Note: http://blog.golang.org/go-slices-usage-and-internals


2022-09-30 15:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.