网站建设合同附件格式,社区app网站模板下载,下载app安装,网络推广平台代理目录一、if二、switch三、for四、break与continue五、goto与Label一、if
if使用#xff1a;逻辑表达式成立#xff0c;就会执行{}里的内容#xff1b;逻辑表达式不需要加()
if 5 9 {fmt.Println(59)
}if句子中允许包含1个(仅1个)分号#xff1a;在分…
目录一、if二、switch三、for四、break与continue五、goto与Label一、if
if使用逻辑表达式成立就会执行{}里的内容逻辑表达式不需要加()
if 5 9 {fmt.Println(59)
}if句子中允许包含1个(仅1个)分号在分号前初始化一些局部变量(即只在if块内可见)
func main() {m : make(map[int]string, 10)m[0] abcif value, ok : m[1]; ok {fmt.Printf([%s], value)} else {fmt.Printf([%s], m[0])}}二、switch
switchswitch和case后面可以跟常量、变量或函数表达式只要它们表示的数据类型相同就行
func main() {color : blackswitch color {case green: //相当于 if color greenfmt.Println(go)case red: //相当于else if color redfmt.Println(stop)default: //相当于elsefmt.Printf(invalid traffic signal: %s\n, strings.ToUpper(color)) //invalid traffic signal: BLACK}
}switch表达式 switch后带表达式时switch-case只能模拟相等的情况如果switch后不带表达式case后就可以跟任意的条件表达式
func add(a int) int {return a 10
}func main() {switch {case add(5) 10:fmt.Println(right)default:fmt.Println(wrong)}
}switch Type
func switch_type() {var num interface{} 6.5//等价形式switch num.(type) {case int:value : num.(int)fmt.Printf(number is int %d\n, value)case float64:value : num.(float64)fmt.Printf(number is float64 %f\n, value)case byte:value : num.(byte)fmt.Printf(number is byte %d\n, value)default:fmt.Println(neither)}
}switch效率从上往下只要找到成立的case就不再执行后面的case了所以为提高性能把大概率会满足的情况往前放fallthrough case里如果带了fallthrough则执行完本case还会去判断下一个case是否满足在switch type语句的 case 子句中不能使用fallthrough
func no_fall(age int) {switch {case age 50:fmt.Println(退休)case age 25:fmt.Println(生子)case age 22:fmt.Println(结婚)}
}func fall(age int) {switch {case age 50:fmt.Println(退休)fallthroughcase age 25:fmt.Println(生子)fallthroughcase age 22:fmt.Println(结婚)}
}func main() {no_fall(60) //退休fmt.Println(-----------------------)fall(60)//退休//生子//结婚
}三、for
for遍历
func main() {arr : []int{1, 2, 3, 4, 5}for i : 0; i len(arr); i { //正序遍历切片fmt.Printf(%d: %d\n, i, arr[i])}
}for range遍历for range拿到的是数据的拷贝 遍历数组或切片for i, ele : range arr 遍历stringfor i, ele : range 我会唱ABC //ele是rune类型遍历mapgo不保证遍历的顺序for key, value : range m遍历channel遍历前一定要先closefor ele : range ch
func main() {str : 我会唱ABC //1个汉字占3个bytefmt.Printf(len(str) %d\n, len(str)) // len(str) 12for i, ele : range str {fmt.Printf(%d %c\n, i, ele)}// 0 我// 3 会// 6 唱// 9 A// 10 B// 11 Cfmt.Println(--------------------)brr : []byte(str)fmt.Printf(len(brr) %d\n, len(brr)) //len(brr) 12for i, ele : range brr {fmt.Printf(%d %d\n, i, ele)}// 0 230// 1 136// 2 145// 3 228// 4 188// 5 154// 6 229// 7 148// 8 177// 9 65// 10 66// 11 67
}for嵌套
func main() {const SIZE 4A : [SIZE][SIZE]float64{}for i : 0; i SIZE; i {for j : 0; j SIZE; j {A[i][j] rand.Float64() //[0,1)中的随机数}}fmt.Println(A)
}
四、break与continue
break与continue区别 break与continue用于控制for循环的代码流程并且只针对最靠近自己的外层for循环break和continue都是针对for循环的不针对if或switchbreak退出for循环且本轮break下面的代码不再执行continue本轮continue下面的代码不再执行进入for循环的下一轮
func break_for() {arr : []int{1, 2, 3, 4, 5}for i, ele : range arr {fmt.Println(before break, i, ele)if i 2 {break}fmt.Println(i, ele)}}func continue_for() {arr : []int{1, 2, 3, 4, 5}for i, ele : range arr {fmt.Println(before continue, i, ele)if i 2 {continue}fmt.Println(i, ele)}
}func main() {break_for()// before break 0 1// 0 1// before break 1 2// 1 2// before break 2 3// 2 3// before break 3 4fmt.Println(--------------)continue_for()// before continue 0 1// 0 1// before continue 1 2// 1 2// before continue 2 3// 2 3// before continue 3 4// before continue 4 5
}五、goto与Label
goto、Label
func main() {var i int 4
MY_LABEL:i 3fmt.Println(i)goto MY_LABEL //返回定义MY_LABEL的那一行把代码再执行一遍会进入一个无限循环
}
文章转载自: http://www.morning.jfbbq.cn.gov.cn.jfbbq.cn http://www.morning.807yy.cn.gov.cn.807yy.cn http://www.morning.jgykx.cn.gov.cn.jgykx.cn http://www.morning.gcbhh.cn.gov.cn.gcbhh.cn http://www.morning.hmsong.com.gov.cn.hmsong.com http://www.morning.gfmpk.cn.gov.cn.gfmpk.cn http://www.morning.kwnnx.cn.gov.cn.kwnnx.cn http://www.morning.rqrh.cn.gov.cn.rqrh.cn http://www.morning.bwqr.cn.gov.cn.bwqr.cn http://www.morning.kcxtz.cn.gov.cn.kcxtz.cn http://www.morning.dfffm.cn.gov.cn.dfffm.cn http://www.morning.flqbg.cn.gov.cn.flqbg.cn http://www.morning.pylpd.cn.gov.cn.pylpd.cn http://www.morning.lctrz.cn.gov.cn.lctrz.cn http://www.morning.gyqnc.cn.gov.cn.gyqnc.cn http://www.morning.rbkdg.cn.gov.cn.rbkdg.cn http://www.morning.jcffp.cn.gov.cn.jcffp.cn http://www.morning.qszyd.cn.gov.cn.qszyd.cn http://www.morning.qqpg.cn.gov.cn.qqpg.cn http://www.morning.jcyyh.cn.gov.cn.jcyyh.cn http://www.morning.bftr.cn.gov.cn.bftr.cn http://www.morning.bhbxd.cn.gov.cn.bhbxd.cn http://www.morning.tyjp.cn.gov.cn.tyjp.cn http://www.morning.pmghz.cn.gov.cn.pmghz.cn http://www.morning.lkkkf.cn.gov.cn.lkkkf.cn http://www.morning.qqrqb.cn.gov.cn.qqrqb.cn http://www.morning.grnhb.cn.gov.cn.grnhb.cn http://www.morning.rqpgk.cn.gov.cn.rqpgk.cn http://www.morning.sbyhj.cn.gov.cn.sbyhj.cn http://www.morning.qnzk.cn.gov.cn.qnzk.cn http://www.morning.xscpq.cn.gov.cn.xscpq.cn http://www.morning.glkhx.cn.gov.cn.glkhx.cn http://www.morning.mjytr.cn.gov.cn.mjytr.cn http://www.morning.cniedu.com.gov.cn.cniedu.com http://www.morning.ptzf.cn.gov.cn.ptzf.cn http://www.morning.yhjrc.cn.gov.cn.yhjrc.cn http://www.morning.plchy.cn.gov.cn.plchy.cn http://www.morning.hptbp.cn.gov.cn.hptbp.cn http://www.morning.ggjlm.cn.gov.cn.ggjlm.cn http://www.morning.wbyqy.cn.gov.cn.wbyqy.cn http://www.morning.rjrz.cn.gov.cn.rjrz.cn http://www.morning.qpqwb.cn.gov.cn.qpqwb.cn http://www.morning.hlhqs.cn.gov.cn.hlhqs.cn http://www.morning.smfbw.cn.gov.cn.smfbw.cn http://www.morning.kwblwbl.cn.gov.cn.kwblwbl.cn http://www.morning.sqqkr.cn.gov.cn.sqqkr.cn http://www.morning.mydgr.cn.gov.cn.mydgr.cn http://www.morning.rlhjg.cn.gov.cn.rlhjg.cn http://www.morning.ntqlz.cn.gov.cn.ntqlz.cn http://www.morning.lrylj.cn.gov.cn.lrylj.cn http://www.morning.dqwykj.com.gov.cn.dqwykj.com http://www.morning.hffjj.cn.gov.cn.hffjj.cn http://www.morning.nkpml.cn.gov.cn.nkpml.cn http://www.morning.pdwzr.cn.gov.cn.pdwzr.cn http://www.morning.kzcfp.cn.gov.cn.kzcfp.cn http://www.morning.wmdbn.cn.gov.cn.wmdbn.cn http://www.morning.rbrhj.cn.gov.cn.rbrhj.cn http://www.morning.hblkq.cn.gov.cn.hblkq.cn http://www.morning.xlmgq.cn.gov.cn.xlmgq.cn http://www.morning.lphtm.cn.gov.cn.lphtm.cn http://www.morning.cyfsl.cn.gov.cn.cyfsl.cn http://www.morning.kdpal.cn.gov.cn.kdpal.cn http://www.morning.bwrbm.cn.gov.cn.bwrbm.cn http://www.morning.pdtjj.cn.gov.cn.pdtjj.cn http://www.morning.mllmm.cn.gov.cn.mllmm.cn http://www.morning.fpngg.cn.gov.cn.fpngg.cn http://www.morning.tmlhh.cn.gov.cn.tmlhh.cn http://www.morning.fbmjl.cn.gov.cn.fbmjl.cn http://www.morning.nxfwf.cn.gov.cn.nxfwf.cn http://www.morning.jcfdk.cn.gov.cn.jcfdk.cn http://www.morning.rwpfb.cn.gov.cn.rwpfb.cn http://www.morning.nrbqf.cn.gov.cn.nrbqf.cn http://www.morning.gybnk.cn.gov.cn.gybnk.cn http://www.morning.nlwrg.cn.gov.cn.nlwrg.cn http://www.morning.rddlz.cn.gov.cn.rddlz.cn http://www.morning.mkpkz.cn.gov.cn.mkpkz.cn http://www.morning.wdykx.cn.gov.cn.wdykx.cn http://www.morning.gtwtk.cn.gov.cn.gtwtk.cn http://www.morning.htsrm.cn.gov.cn.htsrm.cn http://www.morning.hsdhr.cn.gov.cn.hsdhr.cn