"Go" Golang slice array

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

1. Array
A collection of data of the same type
var arr [n]type//Declaration type type one-dimensional array
var arr [m][n]type//Declare type two-dimensional array
multidimensional arrays and so on
can also be used: = declaration
Arr: = [n]type{element 1[, Element 2, ...]} where n can be used "..." Three points indicate that the system determines the number of elements
Subscript can only be of type int, and PHP supports string type subscript
1.1 Number of team leader Len (arr)
Note: The array length is not variable after it is defined
1.2 Traversal:
A.
looping through an array of subscripts access Arr[0] ~ arr[(len)]
b. Range arr, with two return values the first array subscript, and the second as the value of the element, similar to a PHP traversal array

[Plain]View Plaincopyprint?
    1. For k, V: = range Array {
    2. Fmt. Printf ("arr[%d] =%d \ t", K, V)
    3. }
[PHP]View Plaincopyprint?
    1. foreach ($arr as $k = = $v) {
    2. printf ("arr[%d] =%d \ t", $k, $v);
    3. //echo ' $arr ['. $k. "] = " . $v.  "\ t";
    4. }

1.3 When an array is assigned and passed a parameter, it produces an array copy instead of using its pointer

2. Slice
When an array is defined, its length is fixed, and the array is a value type
And slice is a mutable array, but a reference type
2.1 Three ways to generate slice
A.
the declaration is the same as an array, but does not require a specified length
var Slice1 []int
Slice2: = []int {element 1[, Element 2, ...]}
b. Get arr[i:j] from an array (or slice or string) i= array start position, j= end bit result, j-i= slice length, I and J can be omitted, omitted i=0, J=len (arr), I is starting from 0, J is starting from 1
A b c d E F
I 0 1 2 3 4 5
J 1 2 3 4 5 6

[Plain]View Plaincopyprint?
    1. slice1 := arr[:]    //arr[0:6]/arr[0:]  
    2. slice2 := arr[1:1]  //[]  
    3. SLICE4 := ARR3 [1:3] //b c  
    4. Slice5 := arr3[:5]  // = arr3[0:5]   

    C . Make
    Slice1: = Make ([]int, 5, ten)
    len (Slice1) = 5, Cap (SLICE1) = 10, the initial value of the element is 0
  2.2 related Letter Number
    len
(slice): Returns the number of elements in slice (length)
    cap (slice): return slice Allocation space size
    Append (Slice1, Slice2 ...): Append slice2 to Slice1 to generate a new slice, if SLICE2 is a variable, you cannot omit it. ., equivalent to append (Slice1, a[, B, ...])
    copy (target slice, source slice): Copies the source slice to the target slice
  2.3 cap-l, whichever is the minimum number of slice elements En = 0 o'clock, the system will dynamically allocate new array space, that is, the slice will automatically handle the problem of insufficient storage space
  2.4 traversal as array
  2.5 When passing parameters, the pointer is passed
/p>

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.