Deep understanding of arrays and slices _golang in go language

Source: Internet
Author: User
Tags arrays

One, type

An array is a value type, and when an array is assigned to another array, a copy is passed.

A slice is a reference type, and an array of sliced wrappers is called the underlying array of that slice.

Let's look at a piece of code

A is an array, note that the array is a fixed length, the initialization must be specified length, not specified length is sliced a
: = [3]int{1, 2, 3}
//b is an array, is a copy of
B: = a
//c is a slice, is a reference type, the underlying array is a
c: = a[:] for
I: = 0; i < Len (a); i++ {
 a[i] = a[i] + 1
}
//Change A's value, B is a copy, B is unchanged, C is reference, C's Value changes
FMT. Println (a)//[2,3,4]
FMT. Println (b)//[1 2 3]
FMT. Println (c)//[2,3,4]

Two, make

Make can only be used slice , map and channel , so the following section of code generates a slice reference type

S1: = Make ([]int, 0, 3) for

I: = 0; I < caps (S1); i++ {
 S1 = append (S1, i)
}
s2: = S1 for
i: = 0; I < Len (a); i++ {
 S1[i] = s1[i] + 1
}

fmt. Println (S1)//[1 2 3]
FMT. PRINTLN (S2)//[1 2 3]

Third, when the slice append exceeds the bounds of the underlying array

N1 is the underlying array of n2
n1: = [3]int{1, 2, 3}
n2: = N1[0:3]
fmt. PRINTLN ("Address of items in N1:") for
I: = 0; I < Len (N1); i++ {
 FMT. Printf ("%p\n", &n1[i])
}
//address of items in N1:
//0xc20801e160
//0xc20801e168
// 0xc20801e170
FMT. PRINTLN ("Address of items in N2:") for
I: = 0; I < Len (N2); i++ {
 FMT. Printf ("%p\n", &n2[i])
}
//address of items in N2:
//0xc20801e160
//0xc20801e168
// After the 0xc20801e170

//n2 append operation, N2 exceeded the N1 of J
n2 = Append (n2, 1) of the underlying array
. PRINTLN ("Address of items in N1:") for
I: = 0; I < Len (N1); i++ {
 FMT. Printf ("%p\n", &n1[i])
}
//address of items in N1:
//0xc20801e160
//0xc20801e168
// 0xc20801e170

FMT. PRINTLN ("Address of items in N2:") for
I: = 0; I < Len (N2); i++ {
 FMT. Printf ("%p\n", &n2[i])
}
//address of items in N2:
//0xc20803a2d0
//0xc20803a2d8
// 0XC20803A2E0
//0xc20803a2e8

Iv. reference to "failure"

Implements the deletion slice item of the last function

Func Rmlast (A []int) {
 fmt. Printf ("[Rmlast]" The address of A is%p ", a)
 a = A[:len (a)-1]
 FMT. Printf ("[Rmlast] after remove, the address of A is%p", a)
}

After calling this function, we found that the original slice did not change

Func Main () {
 xyz: = []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
 FMT. PRINTF ("[main] the" the address of XYZ is%p\n ", xyz)
 Rmlast (XYZ)
 FMT. PRINTF ("[main] after remove, the address of XYZ is%p\n", xyz)
 FMT. Printf ("%v", xyz)//[1 2 3 4 5 6 7 8 9]
}

The results printed are as follows:

[Main] The "address" of XYZ
is 0xc2080365f0 [rmlast] The address of a
is 0xc2080365f0 [rmlast] After remove, the A Ddress of A is 0xc2080365f0
[main] After remove, the address of XYZ is 0xc2080365f0
[1 2 3 4 5 6 7 8 9]

Here the direct print slice of the pointer value, because slice it is a reference type, so the pointer values are the same, we change slice the printed address to see

Func Rmlast (A []int) {
 fmt. Printf ("[Rmlast]" The address of A is%p ", &a)
 a = A[:len (a)-1]
 FMT.  Printf ("[Rmlast] after remove, the address of A is%p", &a)
}
func main () {
 xyz: = []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
 FMT. PRINTF ([main] The address of XYZ is%p\n, &xyz)
 rmlast (XYZ)
 FMT. PRINTF ("[main] after remove, the address of XYZ is%p\n", &xyz)
 FMT. Printf ("%v", xyz)//[1 2 3 4 5 6 7 8 9]
}

Results:

[Main] The "address" of XYZ
is 0xc20801e1e0 [rmlast] The address of a
is 0xc20801e200 [rmlast] After remove, the A Ddress of A is 0xc20801e200
[main] After remove, the address of XYZ is 0xc20801e1e0
[1 2 3 4 5 6 7 8 9]

This time, you can see that when you pass the function slice as a function argument, you actually copy it too, slice because slice it's a pointer, so from the point of view, slice It's a reference type.

Summarize

The above is the entire content of this article, I hope that the study or work to bring some help, if you have questions you can message exchange.

Related Article

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.