Golang array slices (ii)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Introduction to multidimensional arrays

1. Multidimensional static array
Array: = [][]byte{}

Passing an array in a function is a very expensive behavior because passing a variable between functions is always a pass value, so if the variable is an array, then it means passing the entire array, even if it is large and large ...
For a chestnut, create an array of millions of elements, and on a 64-bit machine it needs 8 megabytes of memory to see what happens when we declare it and pass it on:
Copy the code code as follows:

var array [1e6]int
Foo (array)
Func foo (array [1e6]int) {
...
}

Each time Foo is called, 8 megabytes of memory will be allocated on the stack. Once the function returns, the stack is bounced and the memory is freed, requiring 8 megabytes of space each time.
The Go language is certainly not so silly, there is a better way to pass an array in a function, which is to pass pointers to the array so that only 8 bytes of memory are allocated at a time:
Copy the code code as follows:

var array [1e6]int
Foo (&array)
Func foo (array *[1e6]int) {
...
}
But note that if you change the value that the pointer points to in the function, the value of the original array will also be changed. Fortunately slice (slicing) can help us deal with these problems and come together to have a look.

2. Multidimensional Dynamic array
Slice: = [][]byte{}

Create a two-dimensional data group

    slic := [][]int{{10}, {2122}}    fmt.Println("slic", slic)    fmt.Println("slic[0]:", slic[0])    fmt.Println("slic[1]:", slic[1])    fmt.Println("slic[0][0]", slic[0][0])

Operation Result:

    slic [[10] [21 22]]    slic[0]: [10]    slic[1]: [21 22]    slic[0][0] 10    成功: 进程退出代码 0.

Append () method, such as we are now adding an element to slice[0], rebuilding slice[0];slice[1] is not affected because the allocation space of slice[0] is full and needs to be redistributed

Func mulitsliceappend () {s_0: = []int{ One} s_1: = []int{ -, +} m_slice: = [][]int{s_0, s_1} FMT. Println("Initialize",": M_slice", M_slice) s_0[0] =TenFmt. Println("S_0 Changes",": M_slice", M_slice) FMT. Println("---------------------------------------------------------") m_slice[0] = Append (m_slice[0], One) FMT. Println("Append",": M_slice", M_slice) s_0[0] = Ones_1[0] = OneFmt. Println("S_0 1 Changes",": M_slice", M_slice)}

Operation Result:

初始化    :M_Slice [[11] [20 21]]S_0 更改 :M_Slice [[10] [20 21]]---------------------------------------------------------Append   :M_Slice [[10 11] [20 21]]S_0 1更改 :M_Slice [[10 11] [11 21]]成功: 进程退出代码 0.

As you can see, if the address is not reassigned, when you change the initialization slice, the multidimensional slice value changes accordingly, and after slice[0]append, changes to the S_0 value slice[0] Do not change, the slice[0] is redistributed, and the S_1 value is changed slice [1] With the change, the description slice[1] is not reassigned.

Reference:
Http://www.jb51.net/article/56828.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.