在Go中循环两个变量

The following for loop in Go isn't allowed,

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

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

for (int i = 0, j = 1; i < 10; i ++ , j ++) {...} 
dongxixiu9134
dongxixiu9134 Go中for循环中多个变量的可能重复项
2 年多之前 回复
dongzang7182
dongzang7182 提高提出合格问题的门槛很棒。答案的投票也表明了这个问题的质量。
大约 4 年之前 回复
dpndp64206
dpndp64206 作为参考,可以在语言规范中回答所有类似这样的问题。例如,您甚至不需要滚动或搜索来找到为此的规格,我单击了For语句->ForClause->SimpleStmt->Assignment
大约 4 年之前 回复

2个回答

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")
    }
}

Although above Answer is accepted, and it fully satisfy the need. But I would like to contribute some further explanation to it.

Golang Does not support many things which could be done in simple terms. For loop is a most common example of this. The beauty of Go's For loop is that it merges many modern style of looping into one keyword.

Similarly Golang do with Multiple Variable declaration and assignment. According to above mentioned problem, We could solve multi-variable for loop with this simple tool which Golang provides us. If you want to look into further explanation, this question provide further details and way of declaring multiple variables in one statement.

Coming back to for loop, If we want to declare variable of same datatype we can declare them with this

var a,b,c string

but we use short hand in for loop so we can do this for initializing them with same value

  i,j := 0,1

Different Datatypes and Different Values

and if we want to declare different type of variables and want to assign different values we can do this by separating variables names and after := different values by comma as well. for example

 c,i,f,b := 'c',23423,21.3,false

Usage of Assignment Operator

Later on, we can assign values to multiple variables with the same approach.

    x,y := 10.3, 2
    x,y = x+10, y+1

Mixing Struct and Normal types in single statement

Even we can use struct types or pointers the same way. Here is a function to iterate Linked list which is defined as a struct

func (this *MyLinkedList) Get(index int) int {
    for i,list := 0,this; list != nil; i,list = i+1,list.Next{
        if(i==index){
            return list.Val
        }
    }
    return -1
}

This list is defined as

type MyLinkedList struct {
      Val int
      Next *MyLinkedList

 }

Answering to Original Problem

Coming to the origin Question, Simply it could be done

for i, j := 0, 1; i < 10; i, j = i+1, j+1 {
        fmt.Println("i,j",i,j)
    }
dorv56831
dorv56831 (我更改了帖子中的示例,以匹配@AbdulRehman的注释,因为它可以修复语法错误,并且注释表明这正是该示例的意图。)
一年多之前 回复
duanjuda5789
duanjuda5789 不,您错过了答案中的第二个值:“但是我们使用短手for循环,因此我们可以使用相同的值i,j初始化它们:= 0”
一年多之前 回复
dotaer1993
dotaer1993 对于i,j:= 0,1; 请复制代码并将其粘贴到游戏区。 您错过了逗号后的第二个值1。
一年多之前 回复
dousi6192
dousi6192 i,j:= 0是错误的:play.golang.org/p/-mAV-unCOp4分配不匹配:2个变量但1个值
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问