当前位置:网站首页>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])
}
边栏推荐
- 2022.02.28
- Shutter-- page Jump animation
- After nine years of testing, the salary for interviewing Huawei is 10000. Huawei employees: the company doesn't have such a low salary position
- 微软韦青:狗尾巴的故事—数智时代的第一性原理 | 极客时间
- [creation mode] factory method mode
- Idea2021.1 installation tutorial
- 从0到1稳稳掌握大厂主流技术,年后涨薪不是必须的吗?
- 容易让单片机程序跑飞的原因
- MAUI 入门教程系列(1.框架简介)
- File is in use and cannot be renamed solution
猜你喜欢

Learnopongl notes (IV) - Advanced OpenGL II

CF662B Graph Coloring题解--zhengjun
![[creation mode] builder mode](/img/8d/989c69772502a3ccda320025236e46.png)
[creation mode] builder mode

NielsenIQ宣布任命Tracey Massey为首席运营官

Zero foundation self-study software test, I spent 7 days sorting out a set of learning routes, hoping to help you

openGauss AI能力升级,打造全新的AI-Native数据库

从0到1稳稳掌握大厂主流技术,年后涨薪不是必须的吗?

03 _ Transaction isolation: why can't I see it after you change it?

Cf662b graph coloring problem solution

Art plus online school: Sketch common sitting posture test questions, these three angles must be mastered~
随机推荐
【创建型模式】工厂方法模式
[0006] title, keyword and page description
向数据库导入数据?试试COPY FROM STDIN语句
从0到1稳稳掌握大厂主流技术,年后涨薪不是必须的吗?
Arthas实践操作文档记录
openGauss数据库闪回功能验证
06 _ Global lock and table lock: Why are there so many obstacles to adding a field to a table?
leetcode 120. Triangle minimum path sum
LoveLive! Published an AI paper: generating models to write music scores automatically
【创建型模式】抽象工厂模式
导入数据:gs_restore or MERGE INTO? 看看哪款更适合你
Performance of MOS transistor 25n120 of asemi in different application scenarios
04 _ 深入浅出索引(上)
数据库密态等值查询概述及操作
Introduction to thread practice [hard core careful entry!]
前沿科技探究之AI在索引推荐的应用
Interview shock 26: how to stop threads correctly?
【愚公系列】2022年06月 .NET架构班 079-分布式中间件 ScheduleMaster的集群原理
07 _ 行锁功过:怎么减少行锁对性能的影响?
【MongoDB】4. Usage specification of mongodb