Deep analysis of slice slice structure in go language programming _golang

Source: Internet
Author: User

Array into slices

Copy Code code as follows:

A: = [10]int{}
Fmt. Println (a)
S1: = A[:10]//Take the first 10 elements [5:] Take 5-The last element
Fmt. Println (S1)

Slice test
Copy Code code as follows:

A: = []byte{' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' h '}
SA: = A[2:5]
Fmt. Println (String (SA))
SD1: = A[3:5]
Fmt. Println (String (SD1))//See effect

We see this is slice_a pointing Array_ori is actually pointing from C to K we use FMT. Println (Cap (slice_a)) The result is definitely not 3

Try the bottom one yourself.

Copy Code code as follows:

A: = []byte{' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' h '}
SA: = A[2:5]
Fmt. Println (String (SA))
S: = Sa[1:3]
Fmt. Println (string (s))
S2: = Sa[3:5]
Fmt. Println (String (s2))

Slice is an array that points to the bottom, and if multiple slice point to the same one, one changes, and the other changes. Try the bottom one.
Copy Code code as follows:

A: = []int{1, 2, 3, 4, 5}
S1: = A[2:5]
S2: = A[1:3]
Fmt. Println (S1, S2)
S1[0] = 9
Fmt. Println (S1, S2)

Slices are reference types, that is, if the assignment slices to another slice, they all point to the same underlying array. For example, if a function takes a slice parameter, changes to its elements appear in the caller, similar to passing a pointer to an underlying array. So the read function can accept slicing parameters without pointers and counts; the length of the slice determines the upper bound of the readable data. Here is the signature of the File-type Read method of the OS package:

Copy Code code as follows:

Func (file *file) Read (buf []byte) (n int, err OS.) Error)

This method returns the number of bytes read and possible error values. To read the first 32 bytes of a large buffer B, slice (verb) buffer.
Copy Code code as follows:

N, Err: = F.read (buf[0:32])

This slice is commonly used and efficient. In fact, regardless of efficiency, this fragment can also read the first 32 bytes of the buffer.
Copy Code code as follows:

var n int
var err os. Error
For I: = 0; I < 32; i++ {
Nbytes, E: = F.read (buf[i:i+1])//Read one byte.
If nbytes = = 0 | | E!= Nil {
Err = E
Break
}
n + + nbytes
}

As long as it is within the limits of the underlying array, the length of the slice can be changed, just by assigning itself. The size of the slice, which can be obtained with the internal function cap, giving the maximum length available for this slice. The following function adds a value to the slice. If the data exceeds the capacity, the slice is redistributed and the result slice is returned. This function utilizes the fact that Len and Cap are valid for nil slices and return 0.


The use of Apppend

Copy Code code as follows:

A: = Make ([]int, 3, 6)
Fmt. Printf ("%p", a)
A = Append (A, 1, 2, 3)
Fmt. Printf ("%v%p\n", A, a)
A = Append (A, 1, 2, 3)
Fmt. Printf ("%v%p\n", A, a)

We have to return the slices, because although Append can change the elements of the slice, the slice itself (the RUN-TIME data structure holding the pointer, length, and capacity) is passed by value. The idea of adding slices is useful, so it is implemented by built-in functions append.
Copy Code code as follows:

Func Append (Slice, data[]byte) []byte {
L: = Len (slice)
If L + len (data) > Cap (slice) {//Reallocate
Allocate double what ' s needed for future growth.
Newslice: = Make ([]byte, (L+len (data)) *2)
Copy data (could use bytes). Copy ()).
For I, c: = Range Slice {
Newslice[i] = C
}
Slice = Newslice
}
Slice = Slice[0:l+len (data)]
For I, c: = Range Data {
Slice[l+i] = C
}
return slice
}

When the slice element in the append exceeds the pointed capacity, it points back to a new underlying array, so a change in the underlying array does not drive the other changes, try the following code

Copy Code code as follows:

A: = []int{1, 2, 3, 4, 5}
S1: = A[2:5]
S2: = A[1:3]
Fmt. Println (S1, S2)
S2 = append (s2, 1, 2, 2, 3, 3, 4, 5)
S1[0] = 9
Fmt. Println (S1, S2)

Copy
This is a copy of the function, the code below is copied from S2 to S1 and then we'll see the result is [7 8 9 4 5]
If it is copy (S2,S1) The result we see is [1 2 3]
Copy Code code as follows:

S1: = []int{1, 2, 3, 4, 5}
S2: = []int{7, 8, 9}
Copy (S1, S2)
Fmt. Println (S1)

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.