当前位置:网站首页>【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
边栏推荐
- Redis cerebral fissure
- Chenglian premium products donated love materials for flood fighting and disaster relief to Yingde
- npm清理缓存
- sql主從複制搭建
- 【学习笔记】搜索
- B_ QuRT_ User_ Guide(30)
- 三角变换公式
- MySQL tablespace parsing
- Selenium+chromedriver cannot open Google browser page
- Is it reliable to open an account by digging money? Is it safe?
猜你喜欢
SQL Master slave Replication Build
设置cmd的编码为utf-8
城联优品向英德捐赠抗洪救灾爱心物资
js取整的小技巧
B_QuRT_User_Guide(27)
Almost Union-Find(带权并查集)
Redis persistence problem and final solution
Redis02 -- an operation command of five data types for ending redis (it can be learned, reviewed, interviewed and collected for backup)
Software testing and quality final review
设置网页的标题部分的图标
随机推荐
Discussion on the application of GIS 3D system in mining industry
【学习笔记】线性基
Uvcgan: unt vision transformer cycle-consistent Gan for unpropared image-to-image translation
Redis cerebral fissure
B_QuRT_User_Guide(30)
[shangpinhui] project notes
Do you know TCP protocol (2)?
PC端隐藏滚动条
MySQL implements transaction persistence using redo logs
js运算符的优先级
B_QuRT_User_Guide(28)
App automated testing appium tutorial 2 - ADB command
How to choose an account opening broker? Is it safe to open an account online?
Little artist huangxinyang was invited to participate in the Wuhan station of children's unit of Paris Fashion Week
[learning notes] matroid
Preparation for Oracle 11g RAC deployment on centos7
How redis solves cache avalanche, breakdown and penetration problems
Solve NPM err! Unexpected end of JSON input while parsing near
Oracle RAC -- understanding of VIP
Buffer pool in MySQL