#### Go array slice
*** ferry crossing bonds of the wind, the practice also cultivating ***
##### array of
arrays can store multiple same type of data collection, the array is a data type, it is a value type;
an array of how to define?
var array name [array size] data type: var a [5] int
package main import "fmt" func main(){ var intArr [3]int intArr[0] = 10 intArr[1] = 20 intArr[2] = 30 fmt.Println(intArr) }
Array in memory layout:
1. The address of the array can be acquired by an array name: & intArr;
first array element address is 2. The address of the array:;
addresses of the respective elements of the array 3. The array intervals depending on the type of decision: int64-> 8 bytes, int32-> 4 bytes;
4. if not assigned to an array element, the default value is zero-valued element types: int64-> 0, String -> "", float64-> 0;
5. the access elements in the array: use subscript indexing (left and right opening and closing mode), the other languages, beyond the subscript index will be wrong, index out of bounds;
traversing 6. array in two ways: for the traditional way; for range mode;
main Package Import "FMT" FUNC main () { var A [. 5] int A [0]. 1 = A [. 1] 2 = A [2] =. 3 // Default: a [3] = 0, a [4 ] = 0 // subscript out of range: a [5] = 10 // error bounds index fmt.Println (A) // [2. 3. 1 0 0] fmt.Printf ( "IS A address P% \ n-" , & A) // 0xc000078030 fmt.Printf ( "A [0] address IS P% \ n-", & A [0]) // 0xc000078030 fmt.Printf ( "A [. 1] P address IS% \ n-", & A [ . 1]) // 0xc000078038 fmt.Printf ( "a [2] address iS P% \ n-", & a [2]) // 0xc000078040 traversal // array for i: = 0; i < len (a); ++ {I fmt.Println (A [I]) } // for Range embodiment for k, v: = range a { FMT.Println (a [k]) fmt.Println (v) // This value is a copy of the element, it is not recommended in the operating v: or address-assignment; } }
---
array initialization variety of ways:
Import "FMT" FUNC main () { // 1. literal direct assignment var A [. 5] int = [. 5] int {1,2,3,4,5} var B = [. 5] int {6, 7 are , 8,9,0} // 2. array length derived by the compiler var C = [...] int {1,2,3,4,5,6} // 3. The direct assignment index position var d = [...] {int. 1: 1,10: 0,3:. 3} // derived 4. type F: = [...] {int. 1: 1,9: 9, 10 are: 0} FMT. println (A) fmt.Println (B) fmt.Println (C) fmt.Println (D) fmt.Println (F) }
---
array Precautions
1. An array is a collection of a plurality of the same type, once declared or defined, the length of the array will be fixed and not dynamically change;
2 elements in the array can be of any type, is a value type and reference type may be, may not mix;
3. create array assignment without the use of a zero value type;
4. array values that belong to the type, default value is passed the array is passed, the array does not affect between;
5. If you need to change the original array, the array need to pass the address (transfer similar reference);
6. length is part of the array type, when passing parameters to note the length of the array;
main Package Import "FMT" FUNC Test01 (A [. 5] int) { A [. 1] = 20 is fmt.Println (A) // [20 is. 1. 4. 3. 5] } FUNC Test02 (A * [. 5] int) { A [. 1] = 20 is fmt.Println (* A) // [20 is. 1. 4. 3. 5] } FUNC main () { var A = [. 5] int {1,2,3,4,5} Test01 (A) FMT .Println (a) // [2. 1. 4. 3. 5] // to change the values of the original array, the array need to pass the address Test02 (& a) fmt.Println (a) // [20 is. 1. 4. 3. 5] // We need to consider the length of the array when passing parameters // var B = [. 3] int {l, 2,3} // Test01 (B) // error, length of the array is incorrect }
Go in the programming, using an array or not too much, not because the dynamic expansion, the array is generally used as the data has been determined;
##### slice
1 is a reference to the array slice, the slice belonging to a reference type, when the parameter passing, are passed by reference;
similar array using 2 slices: traversal, access, etc.;
3. slice length can be changed dynamically, so that the array can be said dynamic slice;
4 slices substantially syntax:
var slice name [] type
main Package Import "FMT" FUNC main () { // Only herein demonstrates how to use the slice, the slice then describes how the assignment statement later // declare and define an array var a = [5] int { 1,2,3 4, 5} // s reference slice existing array a var a = s [:] // this reference all of the array elements, the array elements may be referenced portion a = s var [. 1:. 3] FMT. printf ( "% T \ n-", S) fmt.Println (S) fmt.Println (len (S)) // length sections. 5 fmt.Println (cap (s)) // 5 slices of the capacity, capacity and length may vary dynamically; }
---
slice layout in memory
1. slice is at reference type;
2 from the bottom perspective, slice is actually a structure (to be later learning)
type struct {Slice
PTR * [2] int
len int
CAP int
}
initialization sections
main Package Import "FMT" FUNC main () { // create an array of Embodiment 1, so that the reference array sections var A = [. 5] int {1,2,3,4,5} var A = S [:] / / the same as the array, or when the value of the assignment, still need to pay attention slice index is not out of range subscripts // S [10] = 0 S [. 4] = 50 fmt.Println (S) // embodiment 2 // slice is a reference type, so the need to make a statement allocates memory var B [] int // make allocate memory, any value can be specified length, is defined according to a data length appropriate recommendation to be stored // make parameters may also define the capacity of the slice, if defined slice capacity, the capacity requirements than the length of the slice B = the make ([] int,. 5) fmt.Println (B) // embodiment 3 // literal mode, the direct assignment statement var c = [] int {1 , } 2,3,4,5 fmt.Println (C) }
1. Option 1: direct reference to an array that already exists, the underlying visible developer;
2. Mode 2: make by slicing created, the program will create an array, but the array is not visible to the developer, the program itself maintenance;
3. mode 3: similar to the make;
---
Notes slice used:
as 1 slice traversal and access to the same array, do not write here;
2. slice defined later, can not be used directly, need to reference an array allocating memory to make or use the slice;
3. append function may be dynamic expansion sections;
4 can use the copy function of copying slices, the slices are independent copy operation on a new slice has no effect on the original slice ;
the slice is a reference type, when the transfer parameters (slice) to the function, is passed by reference;
main Package Import "FMT" FUNC Test01 (A [] int) { A [0] = 100 fmt.Println (A) } FUNC main () { var A = [] {l, 2,3} int fmt.Println ( A) // the append function expansion sections A = append (A,. 4) fmt.Println (A) // copied using the copy function is sliced // note the following points when using the copy function: // 1 target. (dst) must have been declared and initialized . @ 2 if the length is less than the target length of the slice is copied, the length of the target places subject copy, the excess is not copied; var B [] int B = the make ([ ] int, 3) // beyond the element 4, will not be copied copy (B, a) fmt.Println (B) // new slice of the operation will not affect the original slice B [. 1] = 10 FMT. println (A) fmt.Println (B) // reference transmission sections belonging test01 (a) fmt.Println (a) }
---
string and slice
in the study of basic data types we have learned string type, which is connected by a known string together a plurality of bytes;
in fact the underlying string is an array of bytes, it may also slice a string ;
however string is immutable, it is not string [0] = 'x' , this assignment; If changes are required, it is necessary to turn string [] byte slice, then changes are made into a string type;
main Package Import "FMT" FUNC main () { var A = "abcdef" // slicing processing may be performed fmt.Println (A [:. 3]) // not change the value of the string // a [1] = 'f 'this is wrong // // string needs to be converted to [] byte slices can operate var ARR [] byte ARR = [] byte (a) ARR [. 1] =' F ' a = string (ARR) // after the changes are complete will slice into string type fmt.Println (A) // [] byte can only deal with numbers in English, you need to string into dealing with other languages [] rune slice var b = "Hello" var arr2 is [] Rune arr2 is = [] Rune (B) arr2 is [0] = 'I' B = String (arr2 is) fmt.Println (B) }
The latest articles on personal micro-channel public number, welcome everyone's attention, with the exchange of learning