VB.net 2010 视频教程 VB.net 2010 视频教程 go基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > go语言 >
  • go基础教程之Go for循环语句实例

本站最新发布   go从入门到精通|go基础教程
试听地址  
https://www.xin3721.com/eschool/go.html


for语句是Go编程语言中唯一的循环结构。这里有三种基本类型的for循环。

所有的示例代码,都放在 F:\worksp\golang 目录下。安装Go编程环境请参考:http://www.yiibai.com/go/go_environment.html

  1. 最基本的类型,只具有单一条件。
  2. 一个经典的初始化/条件/之后的for循环。
  3. 对于没有条件的函数将重复循环,直到跳出循环或从包闭函数中返回。

也可以继续下一个循环的迭代。

for.go的完整代码如下所示 -

// `for` is Go's only looping construct. Here are
// three basic types of `for` loops.

package main

import "fmt"

func main() {

    // The most basic type, with a single condition.
    i := 1
    for i <= 3 {
        fmt.Println(i)
        i = i + 1
    }

    // A classic initial/condition/after `for` loop.
    for j := 7; j <= 9; j++ {
        fmt.Println(j)
    }

    // `for` without a condition will loop repeatedly
    // until you `break` out of the loop or `return` from
    // the enclosing function.
    for {
        fmt.Println("loop")
        break
    }

    // You can also `continue` to the next iteration of
    // the loop.
    for n := 0; n <= 5; n++ {
        if n%2 == 0 {
            continue
        }
        fmt.Println(n)
    }
}
Go

执行上面代码,将得到以下输出结果 -

F:\worksp\golang>go run for.go
1
2
3
7
8
9
loop
1
3
5
//原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/go/golang-for.html
 

相关教程