当前位置:网站首页>Basics of golang -- the difference between slicing and array
Basics of golang -- the difference between slicing and array
2022-06-30 12:52:00 【The hunter is eating meat】
The difference between slices and arrays :
● Slice is a pointer type , Arrays are value types
● The length of the array is fixed , Slicing is not ( Slices are dynamic arrays )
● Slice has one more attribute than array : Capacity (cap)
● At the bottom of the slice is an array
Slice is a pointer type , Arrays are value types , So what's the difference ?
Example
package main
import "fmt"
func main() {
// section
numbers := []int{
1, 2, 3, 4, 5, 6}
for index, value := range numbers {
if index == len(numbers)-1 {
numbers[0] += value
} else {
numbers[index+1] += value
}
}
fmt.Println(" section :", numbers)
// Array
numbers2 := [...]int{
1, 2, 3, 4, 5, 6}
for index, value := range numbers2 {
if index == len(numbers2)-1 {
numbers2[0] += value
} else {
numbers2[index+1] += value
}
}
fmt.Println(" Array :", numbers2)
}
section , Each element becomes the sum of the changed value of the previous element and the current element ;
Array , Each element becomes the sum of the current element and the previous element .
The result is :
section : [22 3 6 10 15 21]
Array : [7 3 5 7 9 11]
analysis :
for loop ,numbers The element in is {1,2,3,4,5,6}
. receive for The variables of the loop are index
and value
,index
All are [0,1,2,3,4,5]
.
and value
Different , The array is Value passed , So when traversing an array ,value
by {1,2,3,4,5,6}
;
The slice passes The pointer , So add up every time , It's all changed Value accumulation .
边栏推荐
- Unity脚本的基础语法(1)-游戏对象的常用操作
- How to select an OLAP database engine?
- After the market value evaporated by 65billion yuan, the "mask king" made steady medical treatment and focused on condoms
- Mysql判断计算结果,除以100
- 【C】深入理解指针、回调函数(介绍模拟qsort)
- Four ways for flinksql to customize udtf
- 【一天学awk】内置变量的使用
- 微信小程序报错:TypeError: Cannot read property ‘setData‘ of undefined
- 【OpenGL】OpenGL Examples
- Determining the subject area of data warehouse construction
猜你喜欢
Js根据相同值将数组转换为二维数组
资源变现小程序开通微信官方小商店教程
postman 自动生成 curl 代码片段
Derivation of Park transformation formula for motor control
QT read / write excel--qxlsx worksheet display / hide status setting 4
FlinkSQL自定义UDTF使用的四种方式
商品服务-平台属性
Tencent two sides: @bean and @component are used on the same class. What happens?
Visual studio configures QT and implements project packaging through NSIS
Database usage in QT
随机推荐
江西财经大学智慧江财登录分析
rpm2rpm 打包步骤
Terms related to JMeter performance test and performance test passing standards
黑马笔记---常用日期API
Tencent cloud Database Engineer competency certification was launched, and people from all walks of life talked about talent training problems
Apple executives openly "open the connection": Samsung copied the iPhone and only added a large screen
Qt读写Excel--QXlsx工作表显示/隐藏状态设置4
基于ThinkPHP5封装-tronapi-波场接口-源码无加密-可二开--附接口文档-作者详细指导-2022年6月30日08:45:27
Definition of variables and assignment of variables in MySQL
The spiral matrix of the force buckle rotates together (you can understand it)
JMeter之性能测试流程及性能测试关注点
电机控制park变换公式推导
Sqlserver query code is 936 simplified Chinese GBK. Should I write 936 or GBK?
Visual studio configures QT and implements project packaging through NSIS
【一天学awk】运算符
Js根据相同值将数组转换为二维数组
ERROR: Cannot uninstall ‘PyYAML‘. It is a distutils installed project and thus we cannot accurately
【一天学awk】基础中的基础
波卡跨链通信源码探秘: 要素篇
Solve numpy core._ exceptions. Ufunctypeerror: UFUNC 'Add' did not contain a loop with signature matching