go语言快速入门:流程控制(7)

go语言中没有do-while,switch更加灵活,支持初始化子语句,这篇文章中将会就go的循环/分支等流程控制语句进行简单介绍。

if语句

写法和其他语言基本一致,不过多层else if的时候需要注意{要写在一行,否则通不过编译也无法进行go fmt

例子代码

[root@liumiaocn goprj]# cat basic-if.go
package main

func main() {

        //pattarn 1
        score := 75
        if score < 60 {
                println("Grade: D")
        } else if score < 70 {
                println("Grade: C")
        } else if score < 80 {
                println("Grade: B")
        } else if score <= 100 {
                println("Grade: B")
        } else {
                println("Grade: undefined")
        }

        //pattarn 2
        if score := 45; score < 60 {
                println("Grade: D")
        } else if score < 70 {
                println("Grade: C")
        } else if score < 80 {
                println("Grade: B")
        } else if score <= 100 {
                println("Grade: B")
        } else {
                println("Grade: undefined")
        }
}
[root@liumiaocn goprj]#

执行结果

[root@liumiaocn goprj]# go run basic-if.go
Grade: B
Grade: D
[root@liumiaocn goprj]#

switch-case语句

例子代码

[root@liumiaocn goprj]# cat basic-switch.go
package main

import "fmt"

func main() {
        //pattarn 1
        weekday := "FRI"
        switch weekday {
        case "MON":
                fmt.Println("Monday...")
        case "TUE":
                fmt.Println("Tuesday...")
        case "WED":
                fmt.Println("Wednesday...")
        case "THU":
                fmt.Println("Thursday...")
        case "FRI":
                fmt.Println("Friday...")
        case "SAT":
                fmt.Println("Saterday...")
        case "SUN":
                fmt.Println("Sunday...")
        default:
                fmt.Println("Default branche...")
        }

        //pattarn 2
        switch weekday := "SUN"; weekday {
        case "MON":
                fmt.Println("Monday...")
        case "TUE":
                fmt.Println("Tuesday...")
        case "WED":
                fmt.Println("Wednesday...")
        case "THU":
                fmt.Println("Thursday...")
        case "FRI":
                fmt.Println("Friday...")
        case "SAT":
                fmt.Println("Saterday...")
        case "SUN":
                fmt.Println("Sunday...")
        default:
                fmt.Println("Default branche...")
        }

        //pattarn 3
        score := 75
        switch {
        case score < 60:
                fmt.Println("Grade D...")
        case score <= 70:
                fmt.Println("Grade C...")
        case score <= 80:
                fmt.Println("Grade B...")
        case score <= 90:
                fmt.Println("Grade B...")
        default:
                fmt.Println("Default branche...")
        }

        //pattarn 4
        var obj interface{}
        obj = "Hello World"
        switch obj.(type) {
        case int:
                fmt.Println("This is a int type ...")
        case string:
                fmt.Println("This is a string type...")
        default:
                fmt.Println("Default branch...")
        }
}
[root@liumiaocn goprj]#

执行结果

[root@liumiaocn goprj]# go run basic-switch.go
Friday...
Sunday...
Grade B...
This is a string type...
[root@liumiaocn goprj]#

for语句

例子代码

[root@liumiaocn goprj]# cat basic-for.go
package main

func main() {
        //pattarn 1
        for level := 0; level < 10; level++ {
                for cnt := 0; cnt < 10-level; cnt++ {
                        print(" ")
                }
                for ascnt := 0; ascnt < level; ascnt++ {
                        print("*")
                }
                println("")
        }

        //pattarn 2
        level := 0
        for level < 10 {
                for cnt := 0; cnt < 10-level; cnt++ {
                        print(" ")
                }
                for ascnt := 0; ascnt < 2*level-1; ascnt++ {
                        print("*")
                }
                println("")
                level++
        }

        //pattarn 3
        /*
                for {
                        println("please don try this one unless you want to test your cpu usage")
                }
        */

}
[root@liumiaocn goprj]#

执行结果

[root@liumiaocn goprj]# go run basic-for.go

         *
        **
       ***
      ****
     *****
    ******
   *******
  ********
 *********

         *
        ***
       *****
      *******
     *********
    ***********
   *************
  ***************
 *****************
[root@liumiaocn goprj]#
发布了920 篇原创文章 · 获赞 1260 · 访问量 389万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览