zoukankan      html  css  js  c++  java
  • 【go学习笔记】四、条件和循环

    循环

    Go语言仅支持循环关键字 for

    for i := 0; i<5; i++
    

    示例

    while 条件循环

    while(n<5)

    n := 0
    for n < 5 {
      n++
      fmt.Println(n)
    }
    

    while 无限循环

    while(true)

    for {
      ...
    }
    
    package loop
    
    import "testing"
    
    func TestWhileLoop(t *testing.T)  {
        n:=0
        for n<5{
            t.Log(n)
            n++
        }
    }
    

    输出

    === RUN   TestWhileLoop
    --- PASS: TestWhileLoop (0.00s)
        loop_test.go:8: 0
        loop_test.go:8: 1
        loop_test.go:8: 2
        loop_test.go:8: 3
        loop_test.go:8: 4
    PASS
    
    Process finished with exit code 0
    

    if 条件

    if condition {
        // code to be executed if condition is true
    } else {
        // code to be executed if condition is false
    }
    
    if condition-1 {
        // code to be executed if condition-1 is true
    } else if condition-2 {
        // code to be executed if condition-2 is true
    } else {
        // code to be executed if condition is false
    }
    
    • condition 表达式结果必须为布尔值
    • 支持变量赋值
    if var declaration; condition {
        // code to be executed if condition is true
    }
    

    示例代码请访问: https://github.com/wenjianzhang/golearning

  • 相关阅读:
    UIImage的使用
    UIImageVIew的使用
    关于View和VIewController的关系和理解
    ZT Android 4.2蓝牙介绍
    2013深秋红土地旅行计划之井冈山
    系统性能优化
    Reorder List
    Word Break II
    iOS.CocoaPods.0
    Python 知识要点:案例:士兵突击
  • 原文地址:https://www.cnblogs.com/zhangwenjian/p/12037737.html
Copyright © 2011-2022 走看看