Arrays and array slice Go language

Go in the array and the C array as defined but different methods

c: int a[10][10]

Go [10][10]int

Define and initialize array1: = [5] int {1,2,3,4,5}

Variable name: = [index] {Type} or not added

 

1. Access elements: 1 Like C language to access through the array

     2. The range accessed by key: for i, v: = range array {

                    fmt.Printlb(*Array element[",i,"]=*,v)

                   } The first subscript represents from 0 to len (array) -1

 

2. Value Type: Note that the array is a value type. All types of variable values ​​at the time of the assignment and passed as a parameter will be a copy, the copy will be delivered in the past. So the body of the function can not be changed by modifying the array passed in an array of content.

 

3. Slice functions: structure function data slice can be abstracted as the following three variables

      1. {pointer to a native array;

       2. The number of elements in the array slice;

       3. The array slice allocated storage space.

}-Based array, the array slices add a series of management functions, can dynamically expand storage space at any time, and can be freely passed without causing the managed elements are copied repeatedly.

  Create an array slice:

    1. Create an array according to the foundation (with the original array):

      Array slices can only use a portion of the array elements or the entire array to wear pieces, and even create a bigger slice than the original array of arrays.

      Support with mySlice: = myArray [first: last] In this way an array of slices to generate C language // like from the beginning to the last-1 first bit sequence. myArray [:] means all.

     2. Direct Creation:

      Use built-in functions make Go language provided () can be used to create an array of flexible sections such as:

        mySlice1: = make ([] int, 5) // create a 5 initial value and the initial element of the array slice 0

        mySlice2: = make ([] int, 5,10) // Ibid increase the storage space of ten elements stay

        mySlice3: = [] int {1,2,3,4,5} // create and initialize the array slice directly element

      Self-induction: 1.make function can be sliced ​​and initial values ​​0 and reserve storage space

           2. Direct array slice assignments without adding make such Slice = [] {} Type

2. Slice through the array and the array:

   1. C language with the same access by index

   2. Rapid traverse range function:

      for i,v in range array{

        Returns the index value i is, v is the value of the return element // return the first index, the second return element value

3. Dynamic changes in elements:

  Dynamically increase or decrease the slice array element is more powerful than an array of features. A plurality of storage array slice force (Capacity) concept = number of elements and different spatial distribution. It can greatly reduce the internal memory array slice reallocation and fast memory transfer frequency, greatly improve performance. Because if memory is greater than the original set of memory, CPU needs to retune a larger memory and storage of the original whole thing to a new memory above. And the CPU does not know how much more so it is necessary for the CPU processing times.

  Go there in such a language built-in function cap () and len () function, cap () function returns the size of the space reserved for the array slice, len () function returns the name suggests has the number of elements.

   If you need to continue to add elements after the element sections have been included in the number of columns, you can use append () function is similar to the comparison with the python. Such as:

    mySlice = append (mySlice, 1,2,3) // append () function of the second parameter is a variable parameter in fact, you can add several elements themselves, and even to the end of an array to another array slice sections append :

        mySlice2: = int [] {1,2,3}

        mySlice = append (mySlice, mySlice2 ...) // you need it later ellipsis, since append () function from the parameters for all the parameters are to be from the second additional parameters. MySlice2 ellipsis is equivalent to passing smashed mySlice = append (mySlice, 1,2,3)

        Array slice automatically handle out of storage space. If the content length exceeds additional storage space currently allocated (either greater than CAP () return value), the array sections will automatically allocate a large enough memory.

 

4. Create an array of array-based slice slice:

  The oldSlice: = [] int {1,2,3,4,5}

    newSlice: = oldSlice [: 3] // based array to create an array slice with [first: last]

   // newSlice oldSlice can create elements included, do not exceed the cap oldSlice () function returns the value, over the element portion is initialized to zero

5. Copy the contents:

  Array slice support built-in functions copy Go language (), is used to copy the contents from one array to another array slice slice. If the array is not as large sections added, according to which the number will be smaller slice of the array of elements you copy

  slice1: = [] {int. 1, 2,. 3,. 4,. 5}
  slice2: = [] {int. 6,. 7,. 8}
  Copy (slice1, slice2) // copy slice2 to slice1, but since the number is less than slice2 so how many how many copy slice1

  copy (slice2, slice1) // copy slice1 to slice2, since the number is greater than slice1 slice2, only the first three copy to slice2

 

Guess you like

Origin www.cnblogs.com/beautiful7/p/12355242.html