当前位置:网站首页>自学golang【3.5go语言数组,range关键字】数组的定义方式,使用for循环遍历一维数组
自学golang【3.5go语言数组,range关键字】数组的定义方式,使用for循环遍历一维数组
2022-07-21 17:01:00 【立志考博士】
目录
1.定义一个整形数组,数量写在类型前面
var arr1 [5]int //定义一个整形数组,数量写在类型前面
2.不定义大小让编译器来数
arr3 := [...]int{2, 4, 6, 8, 10} //不定义大小让编译器来数
3.定义一个4行5列数组并输出
var grid [4][5]int //定义一个4行5列数组并输出
fmt.Println(grid)
4.采用for循环遍历一维数组
//遍历数组
for i := 0; i < len(arr3); i++ { //采用for循环遍历一维数组
fmt.Println(arr3[i])
}
5.使用range遍历数组
for i := range arr3 { //采用range关键字遍历一维数组,与上一行等同
fmt.Println(arr3[i])
}
6.采用range关键字输出下标及其对应的值
for i, v := range arr3 { //采用range关键字输出下标及其对应的值
fmt.Println(i, v)
}
总结
本章代码汇总:
package main
import "fmt"
func main() {
var arr1 [5]int //定义一个整形数组,数量写在类型前面
arr2 := [3]int{1, 3, 5}
arr3 := [...]int{2, 4, 6, 8, 10} //不定义大小让编译器来数
fmt.Println(arr1, arr2, arr3)
var grid [4][5]int //定义一个4行5列数组并输出
fmt.Println(grid)
//遍历数组
for i := 0; i < len(arr3); i++ { //采用for循环遍历一维数组
fmt.Println(arr3[i])
}
for i := range arr3 { //采用range关键字遍历一维数组,与上一行等同
fmt.Println(arr3[i])
}
for i, v := range arr3 { //采用range关键字输出下标及其对应的值
fmt.Println(i, v)
}
}
输出:
[0 0 0 0 0] [1 3 5] [2 4 6 8 10]
[[0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0]]
2
4
6
8
10
2
4
6
8
10
0 2
1 4
2 6
3 8
4 10
运行截图:
边栏推荐
- Pytorch学习(一).深度学习回顾和Pytorch简介
- 欺诈检测案例AND泰坦尼克号获救案例
- R language uses LM function to build multiple linear regression model and build regression model without intercept term (the model does not contain intercept)
- 机器学习之时间序列
- youtube字幕下载
- Data sharing | simple temperature and humidity detection simulation based on SHT11
- char short int等类型变量在内存中占用字节数
- R language tests the significance of correlation coefficient: use cor.test function to calculate the value and confidence interval of correlation coefficient and its statistical significance (if the v
- Some skills of Excel
- How to delete colored lines in Excel
猜你喜欢
随机推荐
Redis的持久化方式RDB和AOF的区别
R语言使用lm函数构建多元回归模型(Multiple Linear Regression)、构建没有截距项的回归模型(模型不包含截距)
存储容量与地址空间的计算
Percona XtraDB Cluster安装
Verilog——篮球24S计时器
MySQL regexp case insensitive solution
R language uses LM function to build multiple linear regression model and build regression model without intercept term (the model does not contain intercept)
Installing MySQL on Linux (CentOS)
Verilog——串行四位加法器和超前四位加法器74HC283
Advanced architects, 16 common principles of microservice design and Governance
Can you do application development without programming? Yes!
R language uses the ggarrange function of ggpubr package to combine multiple images, and uses the ggexport function to save the visual images in PNG format (width parameter specifies width, height par
Fast Fourier transform, Lagrange interpolation, three thousand words with examples, sister chapters, application of FFT and string matching
Explain pytorch visualization tool visdom in detail (I)
原码一位乘法器
R language ggplot2 visualization: use the ggarrange function of ggpubr package to combine multiple images, and use the nrow parameter to specify the number of rows in the combined image
R语言使用ggpubr包的ggarrange函数将多幅图像组合起来、使用ggexport函数将可视化图像保存为tiff格式(width参数指定宽度、height参数指定高度、res参数指定分辨率)
Physical address introduction "recommended collection"
FFmpeg 音频解码(秒懂)
pytorch如何将Variable或Tensor转换为numpy?