当前位置:网站首页>Go-控制语句那些事
Go-控制语句那些事
2022-08-09 09:27:00 【草明】
- for range 循环的副本
- switch 中的 case 语句常量(表达式值)不可以重复
上代码, 自己品
package main
import "fmt"
func main() {
list := []int{
1, 2, 3, 4, 5, 6, 7, 8, 9}
for i := range list {
if i == 3 {
list[i] |= i
}
}
fmt.Println(list) // [1 2 3 7 5 6 7 8 9]
}
package main
import "fmt"
func main() {
list := [...]int{
1, 2, 3, 4, 5, 6, 7, 8, 9} // 数组
maxIndex := len(list) - 1
for i, ele := range list {
// 循环的是数组的副本, ele是副本的
fmt.Println(list[i], ele)
if i == maxIndex {
list[0] += ele
} else {
list[i+1] += ele
}
}
fmt.Println(list) // [10 3 5 7 9 11 13 15 17]
}
/* 1 1 3 2 5 3 7 4 9 5 11 6 13 7 15 8 17 9 [10 3 5 7 9 11 13 15 17] */
package main
import "fmt"
func main() {
list := []int{
1, 2, 3, 4, 5, 6, 7, 8, 9} // 切片
maxIndex := len(list) - 1
for i, ele := range list {
// 循环的是切片的副本, 切面是引用类型, 所以是指针的副本, ele是最新的
fmt.Println(list[i], ele)
if i == maxIndex {
list[0] += ele
} else {
list[i+1] += ele
}
}
fmt.Println(list) // [46 3 6 10 15 21 28 36 45]
}
/* 1 1 3 3 6 6 10 10 15 15 21 21 28 28 36 36 45 45 [46 3 6 10 15 21 28 36 45] */
package main
import "fmt"
func main() {
list := [...]int{
1, 2, 3, 4, 5, 6, 7, 8, 9} // 数组
//list := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} // 切片
switch 1 + 3 {
case list[0], list[1]:
fmt.Println("0 or 1")
case list[2], list[3]:
fmt.Println("2 or 3")
case list[4], list[5]:
fmt.Println("4 or 5")
}
// 2 or 3
}
package main
import "fmt"
func main() {
list := [...]int8{
1, 2, 3, 4, 5, 6, 7, 8, 9} // 数组
//list := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} // 切片
switch 2 {
// int
case list[0]: // in8, 编译错误: invalid case list[0] in switch on 2 (mismatched types int8 and int)
fmt.Println("0")
case list[1]:
fmt.Println("1")
case list[2]:
fmt.Println("2")
}
//编译错误
}
package main
import "fmt"
func main() {
list := [...]int8{
1, 2, 3, 4, 5, 6, 7, 8, 9} // 数组
//list := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} // 切片
switch list[0] {
// int8
case 0: // int, 会转换成 int8
fmt.Println("0")
case 1:
fmt.Println("1")
case 2:
fmt.Println("2")
}
// 1
}
package main
import "fmt"
func main() {
list := [...]int8{
1, 2, 3, 4, 5, 6, 7, 8, 9} // 数组
//list := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} // 切片
switch list[0] {
// int8
case 0:
fmt.Println("0")
case 1:
fmt.Println("1")
case 2:
fmt.Println("2")
case 0: //编译错误: duplicate case 0 in switch. case语句表达式的结果值不能重复. 只有常量才会
fmt.Println("0(2)")
case 5 - 5: //编译错误: duplicate case 5 - 5 (value 0) in switch
fmt.Println("0(5-5)")
}
}
package main
import "fmt"
func main() {
list := [...]int8{
1, 2, 3, 4, 5, 6, 7, 8, 9} // 数组
//list := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} // 切片
switch list[0] {
// int8
case list[0]: // int, 会转换成 int8
fmt.Println("0")
case list[1]:
fmt.Println("1")
case list[0], list[2]:
fmt.Println("2")
case list[0]:
fmt.Println("0")
}
// 0
}
package main
import "fmt"
func main() {
list := interface{
}(byte(127))
switch t := list.(type) {
case uint8, uint16:
fmt.Println("uint8 or uint16")
case byte: // 编译错误duplicate case byte in type switch. type byte = uint8
fmt.Println("byte")
default:
fmt.Println("未知类型", t)
}
}
边栏推荐
猜你喜欢
随机推荐
可以写进简历的软件测试项目实战经验(包含电商、银行、app等)
GBase数据库产生迁移工具假死的原因是什么?
本体开发日记05-努力理解SWRL(Built-Ins)
如何用数组实现环形队列
关于一次性通过CISSP考试的一点经验分享
What are the basic concepts of performance testing?What knowledge do you need to master to perform performance testing?
银联最新测试工程师笔试题目,你能得多少分?
Summary of steps and methods for installing and uninstalling test cases that you must read
接口性能测试方案设计方法有哪些?要怎么去写?
2.字节流
Venture DAO Industry Research Report: Macro and Classic Case Analysis, Model Summary, Future Suggestions
"The camera can't be used" + win8.1 + DELL + external camera + USB drive-free solution
一篇文章让你彻底搞懂关于性能测试常见术语的定义
oracle查看表空间占用情况并删除多余表所占空间
lateral view explode的另一种实现方式
使用Protege4和CO-ODE工具构建OWL本体的实用指南-1.3版本(4.Building An OWL Ontology)
学习栈的心得和总结(数组实现)
本体开发日记04-努力理解protege的某个方面
运行flutter项目时遇到的问题修改flutter为国内镜像
迭代






