GO base of sliced

First, what is sliced

Go language abstract slice of the array.

  • Go the length of the array can not be changed, so that the set is not as useful in certain scenarios, Go provides a flexible and powerful built-in type slice ( "dynamic array");
  • Compared with the slice length of the array is not fixed, the elements may be added, so that the capacity may be increased when additional sections.
  • Slice does not have any data, they are just a reference to an existing array.
  • Compared with the array slice, no need to set the length, not the set value in [], is relatively free
  • From the above concepts for a slice like structure, this structure comprises three elements:

Slice structure:

  • Square pointer to the start position in the array slice specified
  • The length of the square length, SPslice of
  • The maximum length of the square, which is the slice start position to the last position of the array length

Second, the use slices

Slice statement

 

    s1 := make([]int, 5)
    s2 := make([]int, 5, 7)

 

Slice initialization:

 

 

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

len () and CAP () function

1, the length of the slice is the number of slice elements.

2, the bottom sections capacity is the number of elements in the array begins with index created from slice.

3, the slice is indexed, and may be acquired by the length len () method, the slice is provided a method of computing capacity CAP (), it can be up to measure how many slices. [Calculation array CAP () results len () identical]

4、 切片实际的是获取数组的某一部分,len切片<=cap切片<=len数组 

package main

import "fmt"

func main() {
    s1 := make([]int, 5)
    s2 := make([]int, 5, 7)
    printSlice(s1) //length:5,cap=5,slice=[0 0 0 0 0]
    printSlice(s2) //length:5,cap=7,slice=[0 0 0 0 0]
    //创建切片
    nums := []int{1, 2, 3, 4, 5}
    printSlice(nums)
    //从切片中截取小切片
    nums1 := nums[1:4]
    printSlice(nums1) //length:3,cap=4,slice=[2 3 4] 
    nums2 := nums[:3]
    printSlice(nums2) //length:3,cap=5,slice=[1 2 3]
    nums3 := nums[2:]
    printSlice(nums3) //length:3,cap=3,slice=[3 4 5]
}
func printSlice(slc []int) {
    fmt.Printf("length:%d,cap=%d,slice=%v \n", len(slc), cap(slc), slc)
}
View Code

 

 三、切片的常用函数

(五)、append( ) 和 copy( )函数

1、函数append( ):

  • •往切片中追加新元素
  • •可以向 slice里面追加一个或者多个元素,也可以追加一个切片。
  • • append函数会改变slice所引用的数组的内容,从而影响到引用同一数组的 其它slice。
  • •当使用 append追加元素到切片时,如果容量不够(也就是(cap-len)== 0),Go就会创建一个新的内存地址来储存元素(该方式效率较低)。

2、函数copy:*复制切片元素

  • •将源切片中的元素复制到目标切片中,返回复制的元素的个数
  • • copy方法是不会建立源切片与目标切片之间的联系。也就是两个切片不存 在联系,一个修改不影响另一个。
package main

import "fmt"

func main() {
    //创建切片
    nums := []int{1, 2, 3, 4, 5}
    printSlice("nums", nums) //slice=nums,p=0xc00000c5d0,length:5,cap=5,slice=[1 2 3 4 5]
    //append 追加元素
    nums = append(nums, 10)
    printSlice("nums", nums) //slice=nums,p=0xc000014230,length:6,cap=10,slice=[1 2 3 4 5 10]
    a := []int{6, 7, 8, 9}
    //追加数组
    nums = append(nums, a...)
    printSlice("nums", nums) //slice=nums,p=0xc000014230,length:10,cap=10,slice=[1 2 3 4 5 10 6 7 8 9]
    //删除第一个元素
    nums = nums[1:]
    printSlice("nums", nums) //slice=nums,p=0xc000014238,length:9,cap=9,slice=[2 3 4 5 10 6 7 8 9]
    //删除最后一个元素
    nums = nums[:len(nums)-1]
    printSlice("nums", nums) //slice=nums,p=0xc000014238,length:8,cap=9,slice=[2 3 4 5 10 6 7 8]
    //删除中间的元素
    b := int(len(nums) / 2)
    nums = append(nums[:b], nums[b+1:]...)
    printSlice("nums", nums) //slice=nums,p=0xc000014238,length:7,cap=9,slice=[2 3 4 5 6 7 8]

    //切片copy
    nums2 := make([]int, len(nums), cap(nums)*2)
    //copy 的切片没有关联
    copy(nums2, nums)
    printSlice("nums2", nums2) //slice=nums,p=0xc000014238,length:7,cap=9,slice=[2 3 4 5 6 7 8]

}
func printSlice(name string, slc []int) {
    fmt.Printf("slice=%v,p=%p,length:%d,cap=%d,slice=%v \n", name, slc, len(slc), cap(slc), slc)
}
View Code

3、使用make创建切片

package main

import "fmt"
import "strconv"

func main() {
    str := make([]string, 0, 16)
    printSlice("str", str)
    for i := 0; i < 5; i++ {
        str = append(str, strconv.Itoa(i))
    }
    printSlice("str", str)

}
func printSlice(name string, slc []string) {
    fmt.Printf("slice=%v,p=%p,length:%d,cap=%d,slice=%v \n", name, slc, len(slc), cap(slc), slc)
}

 

Guess you like

Origin www.cnblogs.com/jalja/p/11782966.html