当前位置:网站首页>GO基础知识—数组和切片
GO基础知识—数组和切片
2022-07-27 09:35:00 【51CTO】
相同点:
1)只能存储一组相同类型的数据结构
2)都是通过下标来访问,并且有容量长度,长度通过 len 获取,容量通过 cap 获取
区别:
1)数组是定长,访问和复制不能超过数组定义的长度,否则就会下标越界,切片长度和容量可以自动扩容
2)数组是值类型,切片是引用类型,每个切片都引用了一个底层数组,切片本身不能存储任何数据,都是这底层数组存储数据,所以修改切片的时候修改的是底层数组中的数据。切片一旦扩容,指向一个新的底层数组,内存地址也就随之改变
简洁的回答:
1)定义方式不一样 2)初始化方式不一样,数组需要指定大小,大小不改变 3)在函数传递中,数组切片都是值传递。
数组的定义
var a1 [3]int
var a2 [...]int{1,2,3}
切片的定义
var a1 []int
var a2 :=make([]int,3,5)
数组的初始化
a1 := [...]int{1,2,3}
a2 := [5]int{1,2,3}
切片的初始化
b:= make([]int,3,5)
边栏推荐
猜你喜欢

Special exercises for beginners of C language to learn code for the first time

XML概述

Read the paper snunet CD: a densely connected Siamese network for change detection of VHR images

监控神器:Prometheus 轻松入门,真香!

如何在树莓派上安装cpolar内网穿透

Hard core structure, violent interpretation

Nacos配置中心动态刷新数据源

Nacos is used as a registration center

32寸曲面屏显示器写代码太爽了!再送一台!

The command prompt cannot start mysql, prompting system error 5. Access denied. terms of settlement
随机推荐
Nacos is used as a registration center
July training (day 24) - segment tree
会议OA项目之会议排座功能&&会议送审的实现
【微信小程序】农历公历互相转换
July training (day 19) - binary tree
ESP8266-Arduino编程实例-ADC
【武汉理工大学】考研初试复试资料分享
Nccl collective communication --collective operations
[wechat applet] lunar calendar and Gregorian calendar are mutually converted
Binary tree traversal
July training (day 13) - two way linked list
The whole process of principle, simulation and verification of breath lamp controlled by FPGA keys
给自己写一个年终总结,新年快乐!
一骑入秦川——浅聊Beego AutoRouter是如何工作
九种方式,教你读取 resources 目录下的文件路径
二叉树遍历
July training (day 14) - stack
6S parameters
C language exercises
【树莓派】Box相关手册-4 Web代理