Slice the array of Golang

The array 1.Golang

    Array is a basic data structure having a fixed length, as in the C language array Once created, its length does not allow changes in golang, the vacant positions of the array with zeros filled array bounds allowed.

     Some basic operations of the array:

     1. Create an array:

 

main FUNC () { 
	var of arr1 = [...] {1,2,3,4} // int [...] is the number of elements of the default length of the array 
	fmt.Println (len (arr1)) / /. 4 
	of arr1 [. 4] = array bounds. 5 // panic 
	fmt.Println (of arr1) 
	var arr2 is = [10] int {1,2,3,4} 
	fmt.Println (arr2 is) // [. 4. 3. 1 0 2 0 0 0 0 0] 
}

  2. copies the value passed is the array:

main FUNC () { 
	var ARR = [10] int {} 4,5,7,11,8,9 
	fmt.Println (ARR) // [4,5,7,11,8,9,0,0, 0,0] 
	// copy verification value is passed the array 
	The AddOne (ARR) 
	fmt.Println (ARR) // [4,5,7,11,8,9,0,0,0,0] 
} 

FUNC The AddOne (ARR [10] int) { 
	ARR [. 9] = 999999 
	fmt.Println (ARR) // [4,5,7,11,8,9,0,0,0,999999] 
}

The 2.Golang slice (slice) 

 

 

     1. First, look at the source of the slice structure:

type slice struct {
	array unsafe.Pointer
	len   int
	cap   int
}

  slice is a special reference type, but it is itself a structure

       Properties len denotes the number of available elements , read and write operations can not exceed this limit, otherwise it will panic

       Attribute cap represents the maximum capacity expansion , of course, this is not unlimited expansion capacity expansion, which is restricted by the length of the underlying array of array, beyond the length of the array will panic bottom

     2.slice creation:

 

main FUNC () { 
     var ARR = [...] {0,1,2,3,4,5,6} int 
     slice1: ARR = [. 1:. 4:. 5] {// Low: High: max} Up a further expansion element 
     // max exceeds len (ARR) 
     // slice2: ARR = [. 1:. 4:. 7] // panic 
     fmt.Println (slice1) // [l, 2,3] 
     Slice3: slice1 = [. 1: 3: 4] // [2,3] is greater than 4 will panic 
     fmt.Println (Slice3) 
}

 

 The above code creates a length of 7 array arr , while creating an array of arr-based slice slice1 , cited slice array index = index =. 1 to 3 between the elements, while also allowing the maximum expansion of a slice element size Space. If this expansion space is greater than 7 then the program will panic. Finally, create a slice1 extension application based on the slice slice2, it refers to the slice index = 1 to the elements between index = 3, due to the expansion of a maximum slice1 elements, so also slice2 expansion of at most one element, than would panic .

 

 

    Created based on the underlying array slice, in which cap values: len <= cap <= len (ARR) between

    Created based on a slice slice, which cap values ​​between: len (slice1) <= cap <= cap (slice1)

   3.slice make use created

 

main FUNC () { 
     var = Slice the make ([] int, 3,5) // = len. 3, CAP =. 5 
     fmt.Println (Slice) // [0,0,0] 
     slice2: Slice = [:. 5] // slice to achieve the expansion of the slice, the slice length becomes. 5 
     fmt.Println (slice2) // [0,0,0,0,0] 
}

 

   4. The slices are passed as parameters

main FUNC () { 
     var = Slice the make ([] int, 3,5) // = len. 3, CAP =. 5 
     fmt.Println (Slice) // [0,0,0] 
     slice2: Slice = [:. 5] // slice to achieve the expansion of the slice, the slice length becomes. 5 
     fmt.Println (slice2) // [0,0,0,0,0] 

     slice [0] = 999 // here the slice and slice index = 0 position 999 is a position index = 0 because they are referenced in the underlying array 999 
     fmt.Println (Slice) 
     fmt.Println (slice2) 

     the AddOne (Slice) // [8888,0,0] 
     fmt.Println (Slice) / / [8888,0,0] 
     fmt.Println (slice2) // [8888,0,0,0] 
} 

FUNC The AddOne (S [] int) { 
	S [0] = 8888 
	fmt.Println (S) 
}

  Because the essence of the slice is a reference type, it is passed to the function, as a function of operating parameters of the underlying array

 

Additional append 3.Golang slice of ()

 

main FUNC () { 
     var ARR = [...] {1,2,3,4} int 
     fmt.Println (ARR) // [1,2,3,4] 
     Slice: ARR = [:] 
     fmt.Println (slice) // [1,2,3,4] 
     slice = the append (slice, [] int {5,6,7 ...}) // this time slice of the reference address has changed occurred, it refers to the underlying array arr no longer, but rather a new array newarr [1,2,3,4,5,6,7] 
     fmt.Println (Slice) // [1,2,3,4,5,6 , 7] 
     // verify slice reference address is changed 
     slice [0] = 666 
     fmt.Println (ARR) // [1,2,3,4] 
     fmt.Println (slice) // [666,2,3 , 4,5,6,7] 
}

 

  Because of this additional elemental slice beyond the original size of the array , so go inside will help us create a new underlying array, and the slice of the reference address is no longer arr , into a newly created array.

       Another case is when a slice is additionally time does not exceed the size of the original array when its reference address has not changed .

func main() {
     var arr = [6]int{1,2,3,4}
     fmt.Println(arr)  //[1,2,3,4,0,0]
     slice := arr[:4]
     fmt.Println(slice) //[1,2,3,4]
     slice = append(slice,5)
     fmt.Println(arr) //[1,2,3,4,5,0]
     fmt.Println(slice)  //[1,2,3,4,5]
}

4. Summary

    (1) go is an array, but usually with a slice more. The array size can not be changed once created, is greater than the number of elements in the array length of time will fill the seats with 0, which is the same with other languages.

     (2) slice slice can be seen as an array of all the operations, which is a reference data type, a data structure which includes an address underlying array, and an element operative length len length expansion or cap.

     (3) in order to break the expansion cap restrictions slice of unlimited expansion need to use append () function to operate. If the total length of the slice append additional elements not exceeding the total length of the bottom of the array, the address reference slice do not change, whereas the reference address will become the address of the new array.

     (4) slice is an abstract concept, meaning that it is the presence of a sequence structure to facilitate easy some operations, such as search, sort, and the like is added, this is similar to the list python.

 

Guess you like

Origin www.cnblogs.com/alienwu/p/12364390.html