内容简介:很容易解释,goto就是把改变了程序的执行顺序,直接跳到标签的位置开始执行,比如示例中第二种for配合range可以用于读取slice和map的数据另外还有
一、流程控制
- if 和其他语言没什么区别,只是判断语句不加括号,比如:
if integer := 5; integer == 3 {
fmt.Println("The integer is equal to 3")
} else if integer < 3 {
fmt.Println("The integer is less than 3")
} else {
fmt.Println("The integer is greater than 3")
}
- goto 这个就神奇了,用goto跳转到必须在当前函数内定义的标签,上代码比较好:
i := 5
here:
i++
fmt.Println(i)
if i < 10 {
goto here
} else {
goto there
}
fmt.Printf("没有到这里")
there:
fmt.Printf("直接到这里")
输出结果:6 7 8 9 10 直接到这里
很容易解释,goto就是把改变了程序的执行顺序,直接跳到标签的位置开始执行,比如示例中 there 和 her e就是两个标签。特别注意的是标签名是 大小写 敏感的。
-
forgo中的for用法很多,基本你能想到的,试试都可以行的通,主要介绍两种:
第一种是最基本的用法,三个条件语句,当然你也可以定义一个什么的
for a := 5;a<10;a++{
fmt.Println(a)
}
第二种for配合range可以用于读取slice和map的数据
for k,v:=range map {
fmt.Println("map's key:",k)
fmt.Println("map's val:",v)
}
另外还有 break 和 continue 关键字,break跳出循环,continue跳出本次循环。
- switch 示例说明:
i := 5
switch i {
case 1:
println(1)
case 5:
println(5)
case 3:
println(1)
}
//输出5 ,仅执行了第二个case
switch i {
case 1:
println(1)
case 5:
println(5)
fallthrough
case 3:
println(3)
fallthrough
case 4:
println(4)
case 6:
println(6)
}
//输出5 3 4
连个示例的关键在于 fallthrough 不同,有 fallthrough 的会强制执行他的下一个case,没有则不会
二、 函数
待续
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Programming in Haskell
Graham Hutton / Cambridge University Press / 2007-1-18 / GBP 34.99
Haskell is one of the leading languages for teaching functional programming, enabling students to write simpler and cleaner code, and to learn how to structure and reason about programs. This introduc......一起来看看 《Programming in Haskell》 这本书的介绍吧!