美文网首页
Golang 流程控制 循环 函数写法

Golang 流程控制 循环 函数写法

作者: 刘昊2018 | 来源:发表于2018-02-08 10:48 被阅读87次

    学习一门新的编程语言,首先应该掰扯清楚的是,跟自己既有掌握的语言之间写法的区别,之前的文章,介绍了Golang中的变量相关的内容,这篇文章主要讲解以下三个方面的主题。

    • 流程控制
    • 循环
    • 函数

    流程控制

    package main
    
    import (
        "fmt"
    
    )
    
    func main() {
        a := 2
        if a > 2 {
            fmt.Println("hehe")
        }else {
            fmt.Println("xixi")
        }
    
        switch a {
        case 1:
            fmt.Println("step 1")
        case 2:
            fmt.Println("step 2")
        case 3:
            fmt.Println("step 3")
        default:
            fmt.Println("default")
        }
    }
    

    需要注意的是:

    • if后面紧跟的表达式不加括号
    • switch中只有case,不需要break,可以使用default

    这里涉及到一个新的关键字: fallthrough

    a := 1
    switch a {
        case 1:
            fmt.Println("step 1")
            fallthrough
        case 2:
            fmt.Println("step 2")
        case 3:
            fmt.Println("step 3")
        default:
        }
    

    fallthrough表示继续执行下个case,与c,java中不写break行为一样。

    循环

    在go中,只有for,没有while系列。

    遍历字符串的每个字符

    str := "hello"
    
    for index := 0; index < len(str); index++ {
      fmt.Printf("%c\n",str[index])
    }
    

    range 关键字

    rang也可以用来遍历

    for index,s := range str {
      fmt.Printf("%d=%c\n",index,s)
    }
    

    返回的第一个参数是索引,第二个参数为对应的索引对应的数据。

    函数

    go允许我们定义有参函数,无参函数,有返回值函数,无返回值函数,多参数函数,多返回值函数。

    • 无参无返回值
    func main() {
        hellofunc()
    }
    
    func hellofunc() {
        fmt.Println("this is first func")
    }
    
    • 有参无返回值
    func main() {
        hellofunc("this is first func")
    }
    
    func hellofunc(str string) {
        fmt.Println(str)
    }
    
    • 无参有返回值
    func main() {
        f := getPI()
    
        fmt.Printf("%f",f)
    }
    
    func getPI() (pi float32) {
        return 3.14
    }
    
    
    • 有参有返回值
    func main() {
        result := sum(1,2)
    
        fmt.Println(result)
    }
    
    func sum(x int ,y int) (result int)  {
        return x + y
    }
    
    • 传入任意个参数
    func main() {
        result := sum2(1,2,3,4,5,6,7,8,9,10)
    
        fmt.Println(result)
    }
    
    func sum2(args... int) (result int) {
        result = 0
        for _,item := range args {
            result = result + item
        }
        return result
    }
    
    • 返回多个参数
    func main() {
        a,b,c := dataset()
        fmt.Println(a)
        fmt.Println(b)
        fmt.Println(c)
    }
    
    func dataset() (a int , b int , c int) {
        a,b,c = 1,2,3
        return a,b,c
    }
    

    注意

    • 返回值不一定必须要写名称,但是go推荐写上,写上的好处是语义更强,同时可以省略return
    // good
    func dataset() (a int , b int , c int) {
        a,b,c = 1,2,3
        return
    }
    
    func dataset() (int , int ,int) {
        a,b,c := 1,2,3
        return a,b,c
    }
    

    go 是推荐使用第一种写法的。写上返回值的名称,在return可以省略。

    defer

    defer用来处理延迟执行。

    func main() {
        defer defer hellofunc("1")
        hellofunc("2")
        hellofunc("3")
        hellofunc("4")
        hellofunc("5")
    }
    func hellofunc(str string) {
        fmt.Println(str)
    }
    

    执行结果为:

    2
    3
    4
    5
    1
    

    修改代码:

    func main() {
        defer hellofunc("1")
        defer hellofunc("2")
        hellofunc("3")
        hellofunc("4")
        defer hellofunc("5")
    }
    

    执行结果为:

    3
    4
    5
    2
    1
    

    修改代码

    func main() {
        defer hellofunc("1")
        defer hellofunc("2")
        defer hellofunc("3")
        defer hellofunc("4")
        defer hellofunc("5")
    }
    

    执行结果:

    5
    4
    3
    2
    1
    

    defer遵循先进后出的栈结构。

    以上,就是关于go中的流程控制,循环,函数相关知识的总结。

    相关文章

      网友评论

          本文标题:Golang 流程控制 循环 函数写法

          本文链接:https://www.haomeiwen.com/subject/wbizzxtx.html