当前位置:网站首页>Learning go language 0x02: understanding slice
Learning go language 0x02: understanding slice
2022-04-23 11:07:00 【Love blog uncle】
Study Go Language 0x02: To slice Slice The understanding of the
It's just my own understanding , For reference only . If you don't understand it correctly , Please correct . thank you :)
* example 1
The code comes from Go Language journey , There may be a slight change .
package main
import "fmt"
func main() {
s := []int{
2, 3, 5, 7, 11, 13}
fmt.Println(len(s), cap(s),s)
s = s[1:4]
fmt.Println(len(s), cap(s),s)
s = s[:2]
fmt.Println(len(s), cap(s),s)
s = s[1:]
fmt.Println(len(s), cap(s),s)
}
Output :
6 6 [2 3 5 7 11 13]
3 5 [3 5 7]
2 5 [3 5]
1 4 [5]
Interpretation of output :
len(s) yes s The actual number of elements in .
cap(s) It's the capacity of the slice , It's from s The first element of begins ( Start with the subscript of the left boundary ), To Original array The number of the last element in the .
-
6 6 [2 3 5 7 11 13]
originalsby[2 3 5 7 11 13], Capacity is 6, From the subscript 0 To 5. -
3 5 [3 5 7]
s[1:4]It's from[2 3 5 7 11 13]The value of ,s[1:4]The value of is[3 5 7]. The actual number of elements is 3,s[1:4]The first element in is 3,3 The position in the original array is the subscript 1, From the subscript 1 To the subscript in the original array 5, Is the total 5 A place , So the capacity is 5.
Or another way to think about it : The capacity is from the left boundary ( Subscript 1) Start , To the subscript in the original array 5, So the capacity is 5. -
2 5 [3 5]
s[:2]It's from[3 5 7]The value of , therefore ,s[:2]The value of is[3 5].s[:2]The first element of is 3, In the original array, the subscript is 1, To the original array subscript 5, So the capacity is 5 .
Or another way to think about it :s[:2]It doesn't change the left boundary , It's still the left boundary of the last time ( Subscript 1), So the capacity is still 5. -
1 4 [5]
s[1:]It's from[3 5]The value of , therefores[1:]The value of is[5]. Numbers 5 The subscript in the original array is 2, To the subscript in the original array 5, So the capacity is 4 .
Or another way to think about it ,s[1:]Changed the left boundary again , Cumulative changes 2 Second left boundary , Each time the left boundary is 1, They all remove a number , A total of 2 Number , So the subscript is from 2 Start , Until the subscript of the original array 5, Capacity of 4 .
* example 2
package main
import "fmt"
func main() {
s := []int{
2, 3, 5, 7, 11, 13}
printSlice(s)
// Slice the slice to give it zero length.
s = s[:0]
printSlice(s)
// Extend its length.
s = s[:5]
printSlice(s)
// Drop its first two values.
s = s[2:]
printSlice(s)
}
func printSlice(s []int) {
fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
}
Output :
len=6 cap=6 [2 3 5 7 11 13]
len=0 cap=6 []
len=5 cap=6 [2 3 5 7 11]
len=3 cap=4 [5 7 11]
Understanding of output :
-
len=6 cap=6 [2 3 5 7 11 13]
s The original value of is{2, 3, 5, 7, 11, 13}, The number of elements is 6, Capacity is 6 -
len=0 cap=6 []
s[:0]It's from{2, 3, 5, 7, 11, 13}The value of , But because the right boundary is 0, therefores[:0]The value of is[], Is an empty array . becauses[:0]The left boundary is not changed , That is, from the subscript 0 Start , Until the subscript in the original array 5, So the capacity is 6. -
len=5 cap=6 [2 3 5 7 11]
s[:5]Is from the original array{2, 3, 5, 7, 11, 13}Value , From the subscript 0 To the subscript 4, therefores[:5]The value of is[2 3 5 7 11].s[:5]The left boundary is not changed , So the capacity is still 6 .
s[:5]The reason why it can expand , Because last time s Is the capacity of the 6, Expand to 5 Elements , No excess capacity .
Capacity can also be understood in this way :s[:5]The value of is[2 3 5 7 11]. First element 2, The subscript in the original array is 0, Until the subscript of the last element in the original array 5, So the capacity is 6 . -
len=3 cap=4 [5 7 11]
s[2:]Changed the left boundary , From the subscript 2 Start , Removed the subscript 0 and 1 Two numbers of positions , So the capacity is reduced 2, namely cap by 4 . ands[2:]It's from the last time[2 3 5 7 11]Value , therefores[2:]The value of is[5 7 11].
Or another way to think about it :s[2:]The value of is[5 7 11], First element 5 The subscript in the original array is 2, Until the subscript in the original array 5, So the capacity is 4 .
* Reference resources
版权声明
本文为[Love blog uncle]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231102106068.html
边栏推荐
猜你喜欢

Excel · VBA array bubble sorting function

An interesting interview question

Visual common drawing (I) stacking diagram

Visual Road (XII) detailed explanation of collection class

STM32接电机驱动,杜邦线供电,然后反烧问题

Promise details

精彩回顾|「源」来如此 第六期 - 开源经济与产业投资

Constraintlayout layout

Google Earth Engine(GEE)——将原始影像进行升尺度计算(以海南市为例)

Canvas详解
随机推荐
Solutions to common problems in visualization (IX) background color
Alarm scene recognition
使用 PHP PDO ODBC 示例的 Microsoft Access 数据库
MBA-day6 逻辑学-假言推理练习题
R-Drop:更强大的Dropout正则方法
MBA-day5數學-應用題-工程問題
Prevent SQL injection in web projects
Common parameters of ffmpeg command line
GO接口使用
学习 Go 语言 0x02:对切片 Slice 的理解
Cygwin 中的 rename 用法
CUMCM 2021-B:乙醇偶合制备C4烯烃(2)
26. Delete duplicates in ordered array
Differences among restful, soap, RPC, SOA and microservices
Let the LAN group use the remote device
Visual common drawing (V) scatter diagram
升级cpolar内网穿透能获得的功能
mysql中整数数据类型tinyint详解
mysql插入datetime类型字段不加单引号插入不成功
关于JUC三大常用辅助类