Slice - Go Language Study Notes

Outline

A slice is based on the reference data of consecutive segments, is a reference type. Compared with the array, the length of the slice may be modified at run-time, the slice can be considered as a dynamic array of variable length.

Achieve slicing is achieved by one of the underlying array thereon and dynamic location, size. The number of points from the underlying array pointer, access elements (i.e., the length of length) and allowed to grow to the number of elements (i.e., the capacity capacity) composition.

  • Pointer ptr, points to the start position of a slice in the bottom of the array.
  • Size len, for recording the number of elements in the slices.
  • CAP capacity, the maximum capacity of the current slice, i.e. the capacity of the underlying array may be dynamically allocated.

Create and initialize sliced

1. make and slice literals

Use the built-in function make () to create a slice:

var slice []type = make([]type, len, cap) It can also be abbreviated: slice := make([]type, len, cap)

// 只指定长度,那么切片的容量和长度相等
slice1 := make([]string, 5)    // 创建一个字符串切片,其长度和容量都是5个元素

// 分别指定长度和容量创建的切片
slice2 := make([]int, 3, 5)    // 创建一个整型切片,其长度为3个元素,容量为5个元素
复制代码

Create a slice using a slice literal:

var identifier []type

slice1 := []string{"Red", "Blue", "Green", "Yellow", "Pink"} // 长度和容量都是5个元素的字符串切片
slice2 := []int{10, 20, 30} // 长度和容量都是3个元素的整型切片
slice3 := []string{99: ""} // 创建字符串切片,使用空字符串初始化第100个元素
复制代码

And statements declare an array of different sections:
// 创建有3个元素的整型数组
array := [3]int{10,20,30}

// 创建长度和容量都是3的整型切片
slice := []int{10, 20, 30}
复制代码

If a value is specified in [] operator, the array is then created rather than slices.

2. nil and empty sections

After the default statement will be initialized without making slices to nil, a length of 0, examples are as follows:

// 创建nil整型切片
var slice []int
复制代码

Use initialization, a slice by slice can create an empty statement, examples are as follows:

// 使用make创建空的整型切片
slice := make([]int, 0)

// 使用切片字面量创建空的整型切片
slice := []int{}
复制代码

Use sliced

1. Assignment and sliced

Use [] operator can change an element, the following examples:

// 创建一个整型切片
// 其容量和长度都是5个元素
slice := []int{10, 20, 30, 40, 50}

// 改变索引为1的元素的值
slice[1] = 25
复制代码

Use slices to create a slice

// 创建一个整型切片
// 其长度和容量都是5个元素
slice := []int{10, 20, 30, 40, 50}

// 创建一个新切片
// 其长度为2个元素,容量为4个元素
newSlice := slice[1:3]
复制代码

Over a period of two sections which share the same underlying array through different slices you will see different parts of the underlying array.

2. Slice growth

Use append elements to increase the slice

// 创建一个整型切片
// 其长度和容量都是5个元素
slice := []int{10, 20, 30, 40, 50}

// 创建一个新切片
// 其长度为2个元素,容量为4个元素
newSlice := slice[1:3]

// 使用原有的容量来分配一个新元素
// 将新元素赋值为60
newSlice = append(newSlice, 60)
复制代码

Use append while increasing capacity and length of the slice

// 创建一个整型切片
// 其长度和容量都是4个元素
slice := []int{10, 20, 30, 40}

// 向切片追加一个新元素
// 将新元素赋值为50
newSlice := append(slice, 50)
复制代码

3. Slice iteration

Use for range iterative slice

// 创建一个整型切片
// 其长度和容量都是4个元素
slice := []int{10, 20, 30, 40}

// 迭代每一个元素,并显示其值
for index, value := rang slice {
    fmt.Printf{"Index: %d Value: %d\n", index, value}
}

OutPut: 
Index: 0  Value: 10
Index: 1  Value: 20
Index: 2  Value: 30
Index: 3  Value: 40
复制代码

Use a for loop iterations sliced

// 创建一个整型切片
// 其长度和容量都是4个元素
slice := []int{10, 20, 30, 40}

// 从第三个元素开始迭代每个元素
for index := 2; index < len(slice); index++ {
    fmt.Printf("Index: %d Value: %d\n", index, slice[index])
}

Output:
Index: 2 Value: 30
Index: 3 Value: 40
复制代码

Reproduced in: https: //juejin.im/post/5cedebac6fb9a07eff006a14

Guess you like

Origin blog.csdn.net/weixin_34205076/article/details/91428891