当前位置:网站首页>【Go ~ 0到1 】 第二天 6月25 Switch语句,数组的声明与遍历
【Go ~ 0到1 】 第二天 6月25 Switch语句,数组的声明与遍历
2022-06-28 08:16:00 【秋日的晚霞】
【Go ~ 0到1 】 第一天 6月25 Switch语句,数组的声明与遍历
1.Switch 语句
func switchmethod(n int) {
switch n {
case 1, 2, 3, 4, 5:
fmt.Println("上班")
break
case 6, 7:
fmt.Println("放假")
break
default:
fmt.Printf("输入有误 %v", n)
}
2.多层循环嵌套跳出外层循环
在JAVA 中 通常都是通过起别名的方式来跳出外层循环 ,而Go 则是通过 goto + lable 来实现的
//多层循环嵌套
func twocircu() {
for i := 0; i < 100; i++ {
for b := 0; b < 100; b++ {
if b == 2 {
fmt.Println(i, "---", b)
goto SS
}
}
}
SS:
fmt.Println("结束")
}
3 == 比较
== 只能比较相同类型的变量
数组的 元素类型 和 长度 都是数组的类型属性
也就是说 两个数组 如果 长度 或 元素类型不一致的时候 是不能用 == 号比较的
4. 数组的声明
1.静态初始化
// 静态初始化 方式一: 固定长度
arr1 := [10]int{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
fmt.Println(arr1)
// 静态初始化 方式二:自动推断长度
arr2 := [...]byte{
'a', 'b', 'c'}
fmt.Println(arr2)
//静态初始化 方式三:根据索引值,固定值
arr3 := [5]int{
1: 10, 2: 20}
fmt.Println(arr3)
2.动态初始化
//动态初始化
arr4 := [5]int{
}
arr4[0] = 1;
arr4[2] = 2;
fmt.Println(arr4)
3. 数组是值类型
在JAVA中 数组是三种引用类型之一
而在Go中 数组是数值类型
end 练习
1.求数组元素总和
//声明局部变量记录总和 (局部变量必须被使用)
sum := 0;
//静态声明一个数组
numarr := [3]int{
1, 3, 5}
//遍历求和
for _, num := range numarr {
sum += num
}
fmt.Println(sum)
2.找出数组中和为指定值的两个元素下标
//声明数组
nums := [...]int{
1, 2, 3, 4, 5, 6, 7, 8, 9}
//查找元素
for i1, num1 := range nums {
for i2, num2 := range nums {
if n == (num1 + num2) {
fmt.Println(num1, "---", num2)
return i1, i2
}
}
}
return -1, -1
边栏推荐
猜你喜欢

Software testing and quality final review

MySQL implements transaction persistence using redo logs

Unity gets the coordinate point in front of the current object at a certain angle and distance

【js】-【DFS、BFS应用】-学习笔记

Little artist huangxinyang was invited to participate in the Wuhan station of children's unit of Paris Fashion Week

Build an integrated kubernetes in Fedora

PMP从报考到拿证基本操作,了解PMP必看篇

Discussion on the application of GIS 3D system in mining industry

关于在cmd中MySQL不能插中文数据的原因

B_QuRT_User_Guide(28)
随机推荐
AI chief architect 8-aica-gao Xiang, in-depth understanding and practice of propeller 2.0
Oracle view all tablespaces in the current library
MySQL single table access method
MySQL tablespace parsing
【学习笔记】模拟
Introduction, compilation, installation and deployment of Doris learning notes
Software testing and quality final review
匿名页的反向映射
新唐NUC980使用记录:自制开发板(基于NUC980DK61YC)
B_ QuRT_ User_ Guide(30)
Upgrade HDP spark to spark 2.4.8 without upgrading ambari
Airflow2 configuration windows azure SSO details based on oauth2 protocol
Image translation /transformer:ittr: unpaired image to image translation with transformers
Force buckle 1024 video splicing
城联优品向英德捐赠抗洪救灾爱心物资
Sword finger offer 03 Duplicate number in array
B_QuRT_User_Guide(27)
Jenkins' common build trigger and hook services (V)
B_QuRT_User_Guide(28)
[shangpinhui] project notes