当前位置:网站首页>Go Basics - arrays and slices
Go Basics - arrays and slices
2022-07-27 09:41:00 【51CTO】
The same thing :
1) Only one set of data structures of the same type can be stored
2) Are accessed through subscripts , And have capacity length , The length passes through len obtain , Capacity pass cap obtain
difference :
1) Array is fixed length , Access and copy cannot exceed the length of the array definition , Otherwise the subscript will cross the line , Slice length and capacity can be automatically expanded
2) Arrays are value types , Slicing is a reference type , Each slice references an underlying array , The slice itself cannot store any data , The underlying array stores data , So when you modify the slice, you modify the data in the underlying array . Once the slice is expanded , Point to a new underlying array , The memory address changes accordingly
Succinct answer :
1) Defined differently 2) Different initialization methods , Array needs to specify size , The size does not change 3) In function passing , Array slices are value passing .
Definition of array
var a1 [3]int
var a2 [...]int{1,2,3}
Definition of slice
var a1 []int
var a2 :=make([]int,3,5)
Initialization of an array
a1 := [...]int{1,2,3}
a2 := [5]int{1,2,3}
Initialization of slices
b:= make([]int,3,5)
边栏推荐
猜你喜欢

The whole process of principle, simulation and verification of breath lamp controlled by FPGA keys

Sentinel 万字教程 | 文末送书

vscode使用remote-ssh连接以及连接失败的解决方法

C# 给Word每一页设置不同文字水印

电机控制器中的MOS驱动

XML overview

好久不送书,浑身不舒服

Eureka delayed registration of a pit

Voice live broadcast system - Principles to be followed in developing push notifications

Nine ways to read the file path under the resources directory
随机推荐
【CTF】ciscn_2019_es_2
【武汉理工大学】考研初试复试资料分享
July training (day 03) - sorting
快应用自定义进度条
七月集训(第14天) —— 栈
都什么年代了你还在用 Date
vscode使用remote-ssh连接以及连接失败的解决方法
Sentinel ten thousand word tutorial | book delivery at the end of the text
[wechat applet] lunar calendar and Gregorian calendar are mutually converted
语音直播系统——开发推送通知需要遵守的原则
Introduction to memory wall
通俗易懂!图解Go协程原理及实战
会议OA项目之会议排座功能&&会议送审的实现
七月集训(第05天) —— 双指针
July training (day 16) - queue
Nine ways to read the file path under the resources directory
QT | about the problem that QT creator cannot open the project and compile it
WordPress prohibits login or registration of plug-ins with a specified user name [v1.0]
BGP联邦实验
S交换机堆叠方案配置指南