Golang Slice Small note

Source: Internet
Author: User

1-Slice initialization

1Func Printslice (S []int) {2Fmt. Printf ("len=%d cap=%d underlying array:%p,%v\n", Len (s), Cap (s), S, s)3 }4 5 func sliceinit () {6 7     varS1 []int//declares S1, and does not initialize, at this time S1 is nil slice, no underlying array is assigned8     ifS1 = =Nil {9Fmt. Println ("S1 is nil")Ten     } One  AS2: = []int{} -     ifS2 = =Nil { -Fmt. Println ("S2 is nil") the     } -  -S3: = Make ([]int,0) -     ifS3 = =Nil { +Fmt. Println ("S3 is nil!") -     } +  A Printslice (S1) at printslice (S2) - Printslice (S3) - } - Func Main () { - Sliceinit () -}

Out:

 is Nillen=0 cap=0 underlying array:0x0, []len=0 cap=0 underlying array:0x55c988, []len=0 cap=0 underlying array:  0x55c988, []

2 Slice length and capacity

The length of the slice is the number of elements it contains.

The capacity of a slice is the number of its first element starting at the end of its underlying array element

Func Main () {s:= []int{2,3,5,7, One, -} printslice (s)//Slice the Slice to give it zero length.s = s[:0] Printslice (s)//Extend its length.s = s[:4] Printslice (s)//Drop its first and the values.s = s[2:] Printslice (s)}

Out:

len=6cap=6Underlying array:0xc04200a2a0, [2 3 5 7  One  -]len=0cap=6Underlying array:0xc04200a2a0, []len=4cap=6Underlying array:0xc04200a2a0, [2 3 5 7]len=2cap=4Underlying array:0xc04200a2b0, [5 7]

3 Assignment and parameter transfer

1 func Slicet () {2Arraya: = [5]int{ -, $, -, -, -}3Fmt. Printf ("Arraya:%p,%v\n", Arraya, Arraya)4Fmt. Printf ("Arraya:%p,%v\n", &Arraya, Arraya)5 6Fmt. Printf ("Arraya:%p,%v\n", &arraya[0], Arraya)7Fmt. Printf ("Arraya:%p,%v\n", &arraya[1], Arraya)8Fmt. Printf ("Arraya:%p,%v\n", &arraya[2], Arraya)9Fmt. Printf ("Arraya:%p,%v\n", &arraya[3], Arraya)TenFmt. Printf ("Arraya:%p,%v\n", &arraya[4], Arraya) One  ASlice: = arraya[1:3:4] -Fmt. Printf ("Slice:%p,%v\n", slice, slice) -Fmt. Printf ("Slice:%p,%v\n", &slice[0], slice) theFmt. Printf ("Slice:%p,%v\n", &slice[1], slice) -  -slice[0] =0 -Fmt. Printf ("Slice:%p,%v\n", slice, slice) +Fmt. Printf ("Arraya:%p,%v\n", &Arraya, Arraya) -  +}

Out

3Arraya:%!p ([5]int=[ -  $  -  -  -]), [ -  $  -  -  -]4Arraya:0xc04206c030, [ -  $  -  -  -]
6Arraya:0xc04206c030, [ - $ - - -]7Arraya:0xc04206c038, [ - $ - - -]8Arraya:0xc04206c040, [ - $ - - -]9Arraya:0xc04206c048, [ - $ - - -]TenArraya:0xc04206c050, [ - $ - - -]
Slice:0xc04206c038, [ $ -]Slice:0xc04206c038, [ $ -]Slice:0xc04206c040, [ $ -]
-Slice0xc04206c038, [0 -] +Arraya:0xc04206c030, [ - 0 - - -]

The

An array variable represents the entire array, which is not a pointer to the first element (unlike the C-language array) so the array name cannot be printed by%p the address. When an array variable is assigned or passed, the entire array is actually copied. (To avoid copying an array, you can pass a pointer to the array)

6~9

int occupies 8 bytes on 64 machines

12~15

Slice syntax a[Low:high:cap]; CAP indicates tile capacity

The slice name is printed by%p and the address of the underlying array referenced by the slice is output

18~19

Changing the value of a slice element is the corresponding element value of the array to which it is referenced.

1 func Slicet () {2s: = []int{1,2,3,4,5}3Fmt. Printf ("underlying array:%p, slice addr:%p\n", S, &s)4 Changeslice (s)5 FMT. Println (s)6 }7 8Func Changeslice (S []int) {9Fmt. Printf ("underlying array:%p, param slice addr:%p\n", S, &s)Tens[1] = -1 One}

Out:

0xc04206c030 0xc04204a3a0  0xc04206c0300xc04204a400[1 -134 5]

1 func sliceT1 () {2s: = []int{1,2,3,4,5}3Fmt. Printf ("underlying array:%p, slice addr:%p\n", S, &s)4 ChangeSlice1 (s)5 FMT. Println (s)6 }7 8Func ChangeSlice1 (S []int) {9Fmt. Printf ("underlying array:%p, param slice addr:%p\n", S, &s)Tens = Append (S,1) One}

Out:

0xc04200a2a0 0xc0420023e0  0xc04200a2a00xc042002440[12345 ]

When a slice is passed as a function parameter is a value pass, a slice is copied, but the formal parameters and arguments have the same underlying array reference.

More information: https://blog.go-zh.org/go-slices-usage-and-internals

Https://www.jianshu.com/p/030aba2bff41

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.