当前位置:网站首页>【Yugong Series】July 2022 Go Teaching Course 020-Array of Go Containers
【Yugong Series】July 2022 Go Teaching Course 020-Array of Go Containers
2022-07-31 16:39:00 【HUAWEI CLOUD】
一、Go容器之数组
1.什么是数组
数组(Array)是有序的元素序列. 若将有限个类型相同的变量的集合命名,那么这个名称为数组名.组成数组的各个变量称为数组的分量,也称为数组的元素,有时也称为下标变量.用于区分数组的各个元素的数字编号称为下标.数组是在程序设计中,为了处理方便, 把具有相同类型的若干元素按有序的形式组织起来的一种形式. 这些有序排列的同类数据元素的集合称为数组.–百度百科
数组是一段固定长度的连续内存区域.
2.Go语言中数组
在 Go 语言数组一旦声明,数组的大小就确定了,不能修改大小但可以修改数组成员.
Go 语言定义数组的格式如下:
var 数组变量名 [元素数量]T
说明:
- 数组变量名: 定义一个数组的变量名
- 元素数量:定义数组的大小
- T 可以是任意基本类型,甚至可以是数组本身,若为数组,则可以实现多维数组
相关案例:
package mainimport ( "fmt")func main() { // 定义一个变量为 arr, 成员类型为 string, 大小为 3 的数组 var arr [3]string // 赋值操作 arr[0] = "愚公1号" arr[1] = "愚公2号" arr[2] = "愚公3号" fmt.Println(arr)}
3.初始化数组
package mainimport ( "fmt")func main() { // 定义一个变量为 arr, 成员类型为 string, 大小为 3 的数组 var arr = [3]string{"愚公1号", "q愚公2号", "愚公3号"} fmt.Println(arr)}
4.数组的遍历
package mainimport ("fmt")func main() { // 定义一个变量为 arr, 成员类型为 string, 大小为 3 的数组 var arr = [...]string{"愚公1号", "q愚公2号", "愚公3号"} for index, v := range arr { fmt.Printf("index: %d, value: %s\n", index, v) }}
边栏推荐
- Implementing click on the 3D model in RenderTexture in Unity
- How to install CV2 smoothly in Anaconda
- Implementing DDD based on ABP
- Delete table data or clear table
- Baidu cloud web speed playback (is there any website available)
- i.MX6ULL驱动开发 | 33 - NXP原厂网络设备驱动浅读(LAN8720 PHY)
- 2020 WeChat applet decompilation tutorial (can applet decompile source code be used)
- Design and Implementation of Compiler Based on C Language
- 你辛辛苦苦写的文章可能不是你的原创
- Handling write conflicts under multi-master replication (4) - multi-master replication topology
猜你喜欢
Unity 之 图集属性详解和代码示例 -- 拓展一键自动打包图集工具
Graham's Scan method for solving convex hull problems
How to switch remote server in gerrit
Intelligent bin (9) - vibration sensor (raspberries pie pico implementation)
i.MX6ULL驱动开发 | 33 - NXP原厂网络设备驱动浅读(LAN8720 PHY)
tooltips使用教程(鼠标悬停时显示提示)
Qt practical cases (54) - using transparency QPixmap design pictures
基于Redis(SETNX)实现分布式锁,案例:解决高并发下的订单超卖,秒杀
C language "the third is" upgrade (mode selection + AI chess)
After Effects 教程,如何在 After Effects 中调整过度曝光的快照?
随机推荐
The new BMW 3 Series is on the market, with safety and comfort
Delete table data or clear table
牛客 HJ18 识别有效的IP地址和掩码并进行分类统计
基于C语言的编译器设计与实现
Flutter 获取状态栏statusbar的高度
Qt practical cases (54) - using transparency QPixmap design pictures
The arm button controls the flashing of the led light (embedded button experiment report)
server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none 失败
C程序是如何跑起来的01 —— 普通可执行文件的构成
Implementing DDD based on ABP
研发过程中的文档管理与工具
研发过程中的文档管理与工具
Small program: Matlab solves differential equations "recommended collection"
多主复制的适用场景(2)-需离线操作的客户端和协作编辑
多数据中心操作和检测并发写入
【pytorch】1.7 pytorch与numpy,tensor与array的转换
【7.29】代码源 - 【排列】【石子游戏 II】【Cow and Snacks】【最小生成数】【数列】
Summary of the implementation method of string inversion "recommended collection"
Mariabackup实现Mariadb 10.3的增量数据备份
Mariabackup implements incremental data backup for Mariadb 10.3