在Go中循环两个变量 [英] For loop of two variables in Go

查看:97
本文介绍了在Go中循环两个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不允许在Go中使用以下for循环

The following for loop in Go isn't allowed,

for i := 0, j := 1; i < 10; i++, j++ {...}

下面两个变量的for循环的正确等效项是什么?

What's the correct equivalent of the for-loop of two variables below?

for (int i = 0, j = 1; i < 10; i ++ , j ++) {...} 

推荐答案

您没有逗号运算符来连接多个语句,但是您确实有多个分配,因此

You don't have a comma operator to join multiple statements, but you do have multiple assignment, so this works:

package main

import (
    "fmt"
)

func main() {
    for i, j := 0, 1; i < 10; i, j = i+1, j+1 {
        fmt.Println("Hello, playground")
    }
}

这篇关于在Go中循环两个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆