当前位置:网站首页>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]
originals
by[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
边栏推荐
- SWAT - Introduction to Samba web management tool
- MySQL数据库事务transaction示例讲解教程
- 学习 Go 语言 0x08:《Go 语言之旅》中 练习使用 error
- Structure of C language (Advanced)
- Excel · VBA custom function to obtain multiple cell values
- Visualized common drawing (II) line chart
- Pytorch implementation of transformer
- 26. Delete duplicates in ordered array
- Full stack cross compilation x86 completion process experience sharing
- VIM + ctags + cscope development environment construction guide
猜你喜欢
Solutions to common problems in visualization (IX) background color
数据库管理软件SQLPro for SQLite for Mac 2022.30
Intuitive understanding entropy
Go interface usage
Visual solutions to common problems (VIII) mathematical formulas
The songbird document editor will be open source: starting with but not limited to markdown
Mysql8. 0 installation guide
使用 PHP PDO ODBC 示例的 Microsoft Access 数据库
MySQL Router重装后重新连接集群进行引导出现的——此主机中之前已配置过的问题
关于JUC三大常用辅助类
随机推荐
学习 Go 语言 0x04:《Go 语言之旅》中切片的练习题代码
26. 删除有序数组中的重复项
Pycharm
Full stack cross compilation x86 completion process experience sharing
Gets the current time in character format
Excel·VBA自定义函数获取单元格多数值
Which company is good for opening futures accounts? Who can recommend several safe and reliable futures companies?
Go interface usage
mysql中整数数据类型tinyint详解
学习网站资料
Visualization Road (11) detailed explanation of Matplotlib color
Learning website materials
ffmpeg命令行常用参数
Analysis on the characteristics of the official game economic model launched by platoffarm
GO接口使用
数据库管理软件SQLPro for SQLite for Mac 2022.30
Ueditor -- limitation of 4m size of image upload component
Manjaro installation and configuration (vscode, wechat, beautification, input method)
我的创作纪念日
一道有趣的阿里面试题