当前位置:网站首页>GO语言-数组Array
GO语言-数组Array
2022-06-11 15:36:00 【一边学习一边哭】
前言
数组:存储相同数据类型的数据结构
数组是定长的,定义了多大就是多大。操作数组中的数据,可以通过索引来操作。
创建数组语法
数组
var array_name [size]array_type
var array_name = [size]array_type
var array_name = [size]array_type{v1, v2, v3, v4} //如果值比size小,int类型用0填充,string类型用空字符串填充。
var array_name = [size]array_type{index1: v1, index3 : v3}
//size写成... 就会在赋值完成后自动计算数组长度
array_name := [...]array_type{v1, v2, v3, v4}
array_name := [...]array_type{index1: v1, index3 : v3}
多维数组
一维数组:存储数据本身
a1 := [3]int{1,2,3}
二维数组:数组中存储的也是数组
a2 := [3][4]int{ {}, {}, {}}
容量3是指二位数组的容量,存储了3个一维数组;容量4是指一维数组的容量。
func main() {
arr := [3][4]int{
{}, {1, 2, 3, 4}, {}}
fmt.Println(arr)
}
//输出:[[0 0 0 0] [1 2 3 4] [0 0 0 0]]访问数组
数组
使用数组的索引(下标)即可访问数组。
func main() {
array_test := []int{1, 2, 3, 4}
fmt.Println(array_test[0], array_test[3])
}获取数组的长度和容量:len()获取长度、cap()获取容量,数组的长度和容量是相等的。
遍历二维数组
func main() {
arr := [2][4]int{
{}, {1, 2, 3, 4}}
fmt.Println("---数组arr---: \n", arr)
//for循环
fmt.Println("\n---for循环遍历数组arr---:")
for i := 0; i < len(arr); i++ {
for j := 0; j < len(arr[i]); j++ {
fmt.Printf("%d ", arr[i][j])
}
}
//for range
fmt.Println("\n---for range遍历数组arr---:")
for _, arr1val := range arr {
for _, arr2val := range arr1val {
fmt.Printf("%d ", arr2val)
}
}
}
数组的数据类型
值类型/引用类型
数组是值类型。赋值的时候,是将原来的值复制一份给新的值。操作新的值对原本数值没有影响。
数值类型:int float bool string array
引用类型:slice map...
引用类型赋值的时候,新的值是直接操作原来值的内存地址。
Array内存分析
数组存储相同类型的数据,会开辟一个连续的内存。go语言的内存是系统自动分配的,当数组不用的时候,就会由GC垃圾回收机制从内存中回收。
数组的地址,其实就是数组第一个元素的地址。因为数组中的元素类型一样,占用内存大小一样。所以就很容易寻址。
func main() {
array_test := [4]int{1, 2, 3, 4}
fmt.Printf("%p--数组内存地址\n", &array_test)
fmt.Printf("%p--数组中第1个元素内存地址\n", &array_test[0])
fmt.Printf("%p--数组中第2个元素内存地址\n", &array_test[1])
}
边栏推荐
- [0006] title, keyword and page description
- 如何管理并发写入操作?带你快速上手
- 如何预测SQL语句查询时间?
- Devil cold rice # 037 devil shares the ways to become a big enterprise; Female anchor reward routine; Self discipline means freedom; Interpretation of simple interest and compound interest
- 【创建型模式】单例模式
- I have used 20K for 5 years and met all major manufacturers. These high-frequency interview questions have helped you figure out
- 让快递快到来不及退款的,真的不是人
- Arthas实践操作文档记录
- Interview shock 26: how to stop threads correctly?
- Implementation of placing the scroll bar on the top of the uniapp
猜你喜欢

YEF 2022昨日开幕,多网络平台全程免费直播,开启在线技术盛宴!

openGauss 3.0.0版本正式发布,立即体验社区首个轻量版本

从内核代码了解SQL如何解析

【创建型模式】建造者模式

openGauss企业版安装
![[creation mode] single instance mode](/img/80/b90c7358de9670e9b07d28752efee5.png)
[creation mode] single instance mode
![[系统安全] 四十二.Powershell恶意代码检测系列 (4)论文总结及抽象语法树(AST)提取](/img/d9/67ad40ba63de8006b67e51b0c82a84.png)
[系统安全] 四十二.Powershell恶意代码检测系列 (4)论文总结及抽象语法树(AST)提取

Uniapp développe des applets Wechat, de la construction à la mise en ligne

openGauss 多线程架构启动过程详解

使用Cloud DB构建APP 快速入门-快应用篇
随机推荐
Uniapp développe des applets Wechat, de la construction à la mise en ligne
三千字教你使用MOT
我的代码变量名称统一 记录
03 _ Transaction isolation: why can't I see it after you change it?
Shuttle-- common commands
Qcustomplot 1.0.1 learning (3) - plotting quadratic functions
06 _ 全局锁和表锁 :给表加个字段怎么有这么多阻碍?
Unified record of my code variable names
【愚公系列】2022年06月 .NET架构班 078-分布式中间件 ScheduleMaster的Worker集群
05 _ In simple terms index (Part 2)
【创建型模式】抽象工厂模式
Introduction and use of etcd
鼻孔插灯,智商上升,风靡硅谷,3万就成
数据库资源负载管理(下篇)
2022年软件测试的前景如何?需不需要懂代码?
Let me tell you the benefits of code refactoring
【愚公系列】2022年06月 .NET架构班 077-分布式中间件 ScheduleMaster加载程序集定时任务
码农必备SQL调优(上)
[creation mode] single instance mode
了解下openGauss的密态支持函数/存储过程